I’m trying to parse an XML file within a console application. An example of how the code looks:
xml_nl = xml_d.SelectNodes("/exchange/messages/message");
foreach (XmlNode xml_n in xml_nl)
{
XmlNodeList xml_ml = xml_n.SelectNodes("header");
int h = 0;
foreach (XmlNode xml_mhn in xml_ml)
{
if (xml_mhn.ChildNodes.Item(h).Name == "reference")
{
switch (xml_mhn.ChildNodes.Item(h).Attributes["type"].Value)
{
case "CU":
senderRef = xml_mhn.ChildNodes.Item(h).ChildNodes.Item(0)["referencevalue"].InnerText.ToString();
break;
case "AAJ":
bulkRef = xml_mhn.ChildNodes.Item(h).ChildNodes.Item(0).InnerText.ToString();
break;
default:
break;
}
}
h++;
}
An example of the XML itself is this:
<?xml version="1.0" encoding="utf-8"?>
<exchange>
<exchangedetails>
<exchangeref>00073281985000</exchangeref>
<exident>1Q54A3</exident>
<exchangedate>2012-06-22</exchangedate>
<exchangetime>23:24:00</exchangetime>
</exchangedetails>
<messages>
<message id="1">
<header>
<reference type="DM">
<referencevalue>73500534821183387</referencevalue>
</reference>
<reference type="CU">
<referencevalue>28-0686668</referencevalue>
</reference>
<reference type="AAJ">
<referencevalue>28-0686668</referencevalue>
</reference>
<reference type="AQY">
<referencevalue>9024</referencevalue>
</reference>
</header>
</message>
</exchange>
My issue is that it won’t loop through the tags and their childnodes, it takes the first one and then just skips the rest. I’ve probably gotten stuck in some odd train of thought, but I just can’t see where. 😛
Any help would be gladly accepted.
You’re not actually looping through the references.
What you’re doing:
What you need to do: