Linq to XML w3c API

The old way:

Supposing you want to create the following xml tree:

Participants
|
--------------------------------------
|                                                 |
Participant                          Participant
|                                                 |
----------------               ---------------------
|                   |               |                          |
FirstName Lastname  FirstName Lastname


 XmlDocument doc = new XmlDocument();

            XmlElement bookparticipant;
            XmlElement lastname;
            XmlElement firstname;


            XmlElement rootbookparticipant = doc.CreateElement("Participants");
            doc.AppendChild(rootbookparticipant);

            bookparticipant = doc.CreateElement("Participant");
            rootbookparticipant.AppendChild(bookparticipant);


            lastname = doc.CreateElement("lastname");
            lastname.InnerText = "Nguyen";
            bookparticipant.AppendChild(lastname);

            firstname = doc.CreateElement("firstname");
            firstname.InnerText = "chien";
            bookparticipant.AppendChild(firstname);


            // another participant
            bookparticipant = doc.CreateElement("Participant");
            rootbookparticipant.AppendChild(bookparticipant);


            lastname = doc.CreateElement("lastname");
            lastname.InnerText = "van";
            bookparticipant.AppendChild(lastname);

            firstname = doc.CreateElement("firstname");
            firstname.InnerText = "minh";
            bookparticipant.AppendChild(firstname);

            doc.Save(Console.Out);
            Console.WriteLine();

         
            XmlElement childnode = doc.DocumentElement;
            Console.WriteLine(childnode.Name);


           foreach (XmlNode xz in childnode.ChildNodes)
            {
               Console.WriteLine("Name {0}",xz.Name);
               foreach (XmlNode km in xz.ChildNodes)
                   Console.WriteLine("Name {0} and value {1}", km.Name, km.InnerText);

           }
Share on Google Plus

About Chien

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.

0 comments:

Post a Comment