• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 376
  • Last Modified:

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.
  • 3
  • 2
1 Solution
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)

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now