我正在尝试在控制台应用程序中解析 XML 文件。代码外观示例:
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++;
}
XML 本身的一个示例如下:
<?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>
我的问题是它不会循环遍历标签及其子节点,它需要第一个,然后跳过其余的。我可能陷入了一些奇怪的想法,但我就是看不出在哪里。:P
任何帮助都会很乐意接受。