Generate and read XML file

   <Person Age="40">
           <Son Age="3">
           <Son Age="5">
   <Person Age="35">
           <Son Age="2">
           <Daughter Age="5">

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
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.

AlexFMAuthor Commented:
Thanks, but I want to do this using XMLDocument.
AlexFMAuthor Commented:
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,"","");

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

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

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

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

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

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

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

                  //end if

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

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

                  // end foreach 2 loop

                  // end foreach loop

                  //let's try to save the XML document in a file: C:\sample.xml
                  catch (Exception e)

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

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.