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

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.
0
AlexFM
Asked:
AlexFM
  • 3
  • 2
1 Solution
 
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

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