I'm completely new to C# and XML parsing so I like to ask how can I read all nodes within this xml file?
<root>
<info>
<name>
<first>bob</first>
<last>john</last>
<middle>D</middle>
</name>
<age>35</age>
<sex>male</sex>
<id>12345</id>
</info>
<info>
<name>
<first>jack</first>
<last>dawnson</last>
<middle>D</middle>
</name>
<age>23</age>
<sex>male</sex>
<id>23456</id>
</info>
</root>
I could get the value for age,sex,and id but not name or its childnodes.Here is what I got so far?
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlOut);
XmlNodeList node = doc.SelectNodes("/root/Info");
Employee empOne = new Employee();
foreach (XmlNode childNode in node) {
empOne.Age = childNode["age"].InnerText;
empOne.Sex = childNode["sex"].InnerText;
empOne.ID = childNode["id"].InnerText;
foreach (XmlNode node2 in childNode.ChildNodes)
{
empOne.FirstName = node2["first"].InnerText;
empOne.LastName = node2["last"].InnerText;
}
}
No comments:
Post a Comment