Generate and read XML file

Posted on 2006-03-26
Last Modified: 2010-04-16
   <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.
Question by:AlexFM
    LVL 14

    Expert Comment

    LVL 48

    Author Comment

    Thanks, but I want to do this using XMLDocument.
    LVL 14

    Expert Comment

    LVL 48

    Author Comment

    LVL 14

    Accepted Solution

    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)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Article by: Ivo
    Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    6 Experts available now in Live!

    Get 1:1 Help Now