第二天,我在使用 XPath 解析 xml 时遇到了困难,我寻求帮助。
有一个源xml文件:
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<S:Body>
<ns2:getDocSaleListResponse xmlns:ns2="http://services.ru/">
<ns2:list>
<ns2:nsaID>100397431</ns2:nsaID>
<ns2:id>7785846</ns2:id>
<ns2:shiftID>89800</ns2:shiftID>
<ns2:docDate>2020-08-02T23:49:43.961+03:00</ns2:docDate>
<ns2:docNo>7785846</ns2:docNo>
<ns2:entityCreator>STATION</ns2:entityCreator>
<ns2:userName>Швабра</ns2:userName>
<ns2:dateDef>2020-10-02T03:00:06.539+03:00</ns2:dateDef>
<ns2:direction>NORMAL_DIRECTION</ns2:direction>
<ns2:goodsDetails>
<ns2:articleID>9990000001562</ns2:articleID>
<ns2:azsID>100397431</ns2:azsID>
<ns2:id>84514</ns2:id>
<ns2:shiftID>89800</ns2:shiftID>
<ns2:barcode>46214829</ns2:barcode>
<ns2:basePrice>113.00</ns2:basePrice>
<ns2:baseSum>113.00</ns2:baseSum>
<ns2:dmcode>00000046214829uD&Y<7?AB9UFb/p</ns2:dmcode>
<ns2:hostname>mksrv1</ns2:hostname>
<ns2:multiplier>1</ns2:multiplier>
<ns2:position>1</ns2:position>
<ns2:quantity>1.000</ns2:quantity>
<ns2:serialNumber>0327260006003759</ns2:serialNumber>
<ns2:tradePrice>113.00</ns2:tradePrice>
<ns2:tradeSum>113.00</ns2:tradeSum>
<ns2:unitName>шт</ns2:unitName>
<ns2:wdChunkSum>0.00</ns2:wdChunkSum>
</ns2:goodsDetails>
<ns2:hostName>mksrv1</ns2:hostName>
<ns2:loyalityTerminalID>0</ns2:loyalityTerminalID>
<ns2:loyaltyProcessingID>888</ns2:loyaltyProcessingID>
<ns2:loyatyCardNumber/>
<ns2:motherSale>0</ns2:motherSale>
<ns2:motherSaleStateID>0</ns2:motherSaleStateID>
<ns2:motherShiftID>0</ns2:motherShiftID>
<ns2:payCardNo/>
<ns2:payState>TRANSPAYSTATE_DONE</ns2:payState>
<ns2:payTime>TRANSPAYTIME_PREPAY</ns2:payTime>
<ns2:paymentProcessingID>100</ns2:paymentProcessingID>
<ns2:paymentTerminalID>0</ns2:paymentTerminalID>
<ns2:prevSaleStateId>0</ns2:prevSaleStateId>
<ns2:saleBornSource>SALEBORNSOURCE_OPERATOR</ns2:saleBornSource>
<ns2:saleID>7785790</ns2:saleID>
<ns2:saleState>TRANS_ST_EXECUTED</ns2:saleState>
<ns2:wholeDiscount>0.00</ns2:wholeDiscount>
</ns2:list>
</ns2:getDocSaleListResponse>
</S:Body>
</S:Envelope>
ns2:list
可以有几个块
得到他们
XmlDocument xml = new XmlDocument();
xml.LoadXml(result);
XmlElement xRoot = xml.DocumentElement;
XmlNodeList items = xml.GetElementsByTagName("ns2:list");
XmlNamespaceManager xs = new XmlNamespaceManager(xml.NameTable);
xs.AddNamespace("ns2", "http://schemas.xmlsoap.org/soap/envelope/");
接下来,我们尝试拉出嵌套的节点
foreach (XmlNode cx in items)
{
string docDate = cx.SelectSingleNode("ns2:docDate").InnerText;
Console.WriteLine(docDate);
}
无论我如何尝试,我都无法获取参数的值。如果我放置而不是节点名称"*"
,则返回嵌套节点的完整列表。不想按名字搜索。告诉我是什么原因。
全部的: