Generate and read XML file

<List>
   <Person Age="40">
       <FirstName>John</FirstName>
       <LastName>Brown</LastName>
       <Children>
           <Son Age="3">
                <Name>Bill</Name>
           </Son>
           <Son Age="5">
                <Name>Jerry</Name>
           </Son>
       </Children>
   </Person>
   <Person Age="35">
       <FirstName>Tom</FirstName>
       <LastName>Robbins</LastName>
       <Children>
           <Son Age="2">
                <Name>Michael</Name>
           </Son>
           <Daughter Age="5">
                <Name>Mary</Name>
           </Daughter>
       </Children>
   </Person>
</List>

I need code to generate this XML using XMLDocument and save it to file. Then I need to load file to XMLDocument and enumerate all nodes.
LVL 48
AlexFMAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

existenz2Commented:
0
AlexFMAuthor Commented:
Thanks, but I want to do this using XMLDocument.
0
AlexFMAuthor Commented:
0
existenz2Commented:
You want something like the following code. It creates the structure you want, but is missing some for/while loops, since I have no idea what your datasource is. But you should easily create those, since I added the places in comment for you. ;)

The Code:

public static void Write()
            {

                  XmlDocument xmldoc=new XmlDocument();

                  XmlNode xmlnode=xmldoc.CreateNode(XmlNodeType.XmlDeclaration,"","");
                  xmldoc.AppendChild(xmlnode);

                  XmlElement xmlelem=xmldoc.CreateElement("","List","");
                  xmldoc.AppendChild(xmlelem);

                  //foreach loop
                  XmlElement person =xmldoc.CreateElement("","Person","");
                  XmlAttribute personAttr = xmldoc.CreateAttribute("Age");
                  personAttr.Value = 40.ToString();
                  person.Attributes.Append(personAttr);

                  // Person:
                  XmlElement firstName = xmldoc.CreateElement("","FirstName","");
                  XmlText firstNameText = xmldoc.CreateTextNode("John");
                  firstName.AppendChild(firstNameText);


                  XmlElement lastName = xmldoc.CreateElement("","LastName","");
                  XmlText lastNameText = xmldoc.CreateTextNode("Brown");
                  lastName.AppendChild(lastNameText);

                  XmlElement children = xmldoc.CreateElement("","Children","");

                  //foreach loop 2
                  // if son
                  XmlElement son = xmldoc.CreateElement("","Son","");
                  XmlAttribute sonAttr = xmldoc.CreateAttribute("Age");
                  sonAttr.Value = 3.ToString();
                  son.Attributes.Append(sonAttr);

                  XmlElement sonName = xmldoc.CreateElement("","Name","");
                  XmlText sonNameText = xmldoc.CreateTextNode("Bill");
                  sonName.AppendChild(sonNameText);

                  son.Attributes.Append(sonAttr);
                  son.AppendChild(sonName);
                  //end if

                  // if daugther
                  XmlElement daugther = xmldoc.CreateElement("","Daugther","");
                  XmlAttribute daugtherAttr = xmldoc.CreateAttribute("Age");
                  daugtherAttr.Value = 5.ToString();
                  daugther.Attributes.Append(daugtherAttr);

                  XmlElement daugtherName = xmldoc.CreateElement("","Name","");
                  XmlText daugtherNameText = xmldoc.CreateTextNode("Mary");
                  daugtherName.AppendChild(daugtherNameText);
                  daugther.AppendChild(daugtherName);
                  //end if

                  children.AppendChild(son);
                  children.AppendChild(daugther);      
                  // end foreach 2 loop

                  person.AppendChild(firstName);
                  person.AppendChild(lastName);
                  person.AppendChild(children);
                  
                  xmldoc.ChildNodes.Item(1).AppendChild(person);
                  // end foreach loop

                  
                  //let's try to save the XML document in a file: C:\sample.xml
                  try
                  {
                        xmldoc.Save(@"c:\sample.xml");
                  }
                  catch (Exception e)
                  {
                        Console.WriteLine(e.Message);
                  }
                  Console.ReadLine();
            }
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.