Reputation: 9648
I have data in the following format: https://www.dropbox.com/s/osu4w634lnoy2pw/2.xml
While trying to parse I want all the field elements also. I am able to get all the elements under table but not under field. Could somebody please help me?
My code is as follows:
XmlDocument doc = new XmlDocument();
doc.Load(@maploc);
XmlNodeList nodes = doc.DocumentElement.SelectNodes("/schema/table");
foreach (XmlNode node in nodes)
{
attribute1 = "";
attribute2 = "";
attribute3 = "";
try
{
attribute1 = node.Attributes["name"].Value;
attribute2 = node.SelectSingleNode("tabledefault").InnerText;
attribute3 = node.SelectSingleNode("invoke").InnerText;
}
catch(Exception ex)
{
//Nothing
}
if (node.HasChildNodes)
{
for (int i = 0; i < node.ChildNodes.Count; i++)
{
foreach (XmlNode nodei in node.ChildNodes[i])
{
attribute4 = "";
attribute5 = "";
attribute6 = "";
try
{
attribute4 = node.Attributes["name"].Value;
attribute5 = node.SelectSingleNode("invoke").InnerText;
attribute6 = node.SelectSingleNode("dtype").InnerText;
catch (Exception ex)
{
//Nothing
}
}
}
}
Thanks...
Upvotes: 1
Views: 316
Reputation: 23793
Your error is :
for (int i = 0; i < node.ChildNodes.Count; i++)
{
foreach (XmlNode nodei in node.ChildNodes[i])
{
You are iterating over the nodes that are the children of the current child (you are going one level too deep). This is because an XmlNode
is an Enumerable over its Children.
A single foreach
loop is needed :
foreach (XmlNode nodei in node.ChildNodes)
{
Upvotes: 1