这是我的xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<!--Sample XML file generated by XMLSPY v5 rel. 4 U (http://www.xmlspy.com)-->
<decsts:EDI xmlns:decsts="http://www.testurl.com/schema/decsts" xmlns:cm="http://www.testurl.com/schema/common" xmlns:deccm="http://www.testurl.com/schema/deccm" xmlns:sau="http://www.testurl.com/schema/sau" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.testurl.com/schema/decsts
D:\sns\trade\sau\rel1_0\schemas\IDEC\decSTS.xsd" decsts:docType="DEC" decsts:id="SAU20160603187160" decsts:msgType="H2HDECSTS">
<decsts:record>
<sau:payload xsi:type="decsts:declarationStatusType">
<decsts:reference>
<decsts:userid>sdec001001</decsts:userid>
<decsts:acctId>rrni</decsts:acctId>
<decsts:docRefNo>20160603005992</decsts:docRefNo>
<decsts:regPort cm:type="4">23</decsts:regPort>
<deccm:declarationISN>1517791</deccm:declarationISN>
</decsts:reference>
<decsts:statusInformation>
<decsts:statusDate>2016-06-03</decsts:statusDate>
<decsts:statusCode>1</decsts:statusCode>
<decsts:originalMsgId>SAU20160603187153</decsts:originalMsgId>
</decsts:statusInformation>
<decsts:statusErrorDetails>
<decsts:statusErrorCodeDetails>4</decsts:statusErrorCodeDetails>
<decsts:remarks>goods are waiting for collection of customs fees</decsts:remarks>
</decsts:statusErrorDetails>
<decsts:statusErrorDetails>
<decsts:statusErrorCodeDetails>5</decsts:statusErrorCodeDetails>
<decsts:remarks>EXTRA NODE ERR</decsts:remarks>
</decsts:statusErrorDetails>
</sau:payload>
</decsts:record>
</decsts:EDI>
我正在尝试使用以下代码迭代xml文件:
XDocument xdoc = XDocument.Load("TEST_XML.xml");
XNamespace ns = "http://www.TESTURL.com/schema/decsts";
var datas = from query in xdoc.Descendants(ns + "statusErrorDetails")
select new
{
err_no = (string)query.Element(ns + "statusErrorCodeDetails"),
err_desc = (string)query.Element(ns + "remarks")
};
foreach (var item in datas)
{
Console.WriteLine(item.err_no);
Console.WriteLine(item.err_desc);
}
我得到以下输出:
4
错误描述4
为什么它不迭代下一个节点和子的值。请帮我解决这个问题,谢谢。
答案 0 :(得分:0)
我解决了这个问题,我尝试了下面的代码,它给了我想要的输出:
XmlNodeList err_details = xmlDoc.GetElementsByTagName("decsts:statusErrorDetails");
foreach (XmlNode err_node in err_details)
{
foreach (XmlNode err_items in err_node.ChildNodes)
{
Console.WriteLine(err_items.InnerText);
}
}