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);
}
0 comments:
Post a Comment