【发布时间】:2017-02-04 03:16:00
【问题描述】:
我开始使用 XmlDocument 在 C# 中解析 XML。我已经关注 xml
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<ns1:getCustomersResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://DefaultNamespace">
<getCustomersReturn soapenc:arrayType="soapenc:string[2]" xsi:type="soapenc:Array" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
<getCustomersReturn xsi:type="soapenc:string"></getCustomersReturn>
<getCustomersReturn xsi:type="soapenc:string">some data</getCustomersReturn>
<getCustomersReturn xsi:type="soapenc:string">some more data</getCustomersReturn>
</getCustomersReturn>
</ns1:getCustomersResponse>
</soapenv:Body>
</soapenv:Envelope>
我的代码:
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(result); //loading soap message as string
XmlNamespaceManager xmlNamespaceManager = new XmlNamespaceManager(xmlDocument.NameTable);
xmlNamespaceManager.AddNamespace("ns1", "http://DefaultNamespace");
XmlNodeList xmlNodeList = xmlDocument.SelectNodes("//ns1:getCustomersResponse/getCustomersReturn", xmlNamespaceManager);
string[] results = new string[xmlNodeList.Count];
var str = "";
var count = 0;
foreach (XmlNode xmlNode in xmlNodeList)
{
str += xmlNode.InnerText;
}
Assert.IsNull(results, xmlNodeList.Count + ", " + count + ", " + str);
我想将getCustomersReturn 的子节点(文本)检索为string[]
问题:
所有节点都被扁平化为单个字符串而不是数组。
xmlNodeList.Count 是 1 而不是 3
编辑:实施答案后的后续问题
CS1061 'XmlDocument' does not contain a definition for 'SelectNodes' and no extension method 'SelectNodes' accepting a first argument of type 'XmlDocument' could be found (are you missing a using directive or an assembly reference?)
【问题讨论】:
-
有人可以建议或建议而不是投票,所以我知道这个问题有什么问题