Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 解析Soap消息_C#_Xml_Web Services_Soap_Xml Parsing - Fatal编程技术网

C# 解析Soap消息

C# 解析Soap消息,c#,xml,web-services,soap,xml-parsing,C#,Xml,Web Services,Soap,Xml Parsing,我最近开始使用SOAP。 现在我正在尝试用C#解析SOAP消息。 贺电如下: <SOAP-ENV:Body xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> <ns1:getBuildingsResponse xmlns:ns1="http://someserver.net/~username/lab/servis?ws=1"> <return SOAP-ENC:arrayType="

我最近开始使用SOAP。
现在我正在尝试用C#解析SOAP消息。
贺电如下:

<SOAP-ENV:Body xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
  <ns1:getBuildingsResponse xmlns:ns1="http://someserver.net/~username/lab/servis?ws=1">
    <return SOAP-ENC:arrayType="ns2:Map[2]" xsi:type="SOAP-ENC:Array" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
  <item xsi:type="ns2:Map">
    <item>
      <key xsi:type="xsd:string">id</key>
      <value xsi:type="xsd:string">1</value>
    </item>
    <item>
      <key xsi:type="xsd:string">code</key>
      <value xsi:type="xsd:string">345-GESG</value>
    </item>
    <item>
      <key xsi:type="xsd:string">name</key>
      <value xsi:type="xsd:string">Building 1</value>
    </item>
  </item>
  <item xsi:type="ns2:Map">
    <item>
      <key xsi:type="xsd:string">id</key>
      <value xsi:type="xsd:string">7590913</value>
    </item>
    <item>
      <key xsi:type="xsd:string">code</key>
      <value xsi:type="xsd:string">353-gr</value>
    </item>
    <item>
      <key xsi:type="xsd:string">name</key>
      <value xsi:type="xsd:string">Building 2</value>
    </item>
  </item>
</return>
问题是,我不知道如何处理没有名称空间的标记。。。 这行代码:

Console.WriteLine(xNodelst.Count); 
始终打印0,但我希望它打印2,因为数组中有2个元素(ns2:Map[2])。
也就是说,我想循环所有这些元素:

<item xsi:type="ns2:Map">

任何帮助都将不胜感激

XmlDocument xdoc = new XmlDocument();
    xdoc.LoadXml(client.Invoke("getBuildings").ToString());    
    XmlNamespaceManager nsmgr = new XmlNamespaceManager(xdoc.NameTable);    
    XmlNodeList nodes = xDoc.SelectNodes("//item[@xsi:type='ns2:Map']",nsmgr);
    var nodeCount=nodes.Count;

试试这个,这可能会对您有所帮助。

我编辑它后,它正在工作:
XmlNodeList nodest nodes=xDoc.SelectNodes(//item[@xsi:type='ns2:Map'],nsmgr)
Console.WriteLine(nodes.Count)//打印2
还有一个问题:如何从每个项目中提取键和值字段?
XmlDocument xdoc = new XmlDocument();
    xdoc.LoadXml(client.Invoke("getBuildings").ToString());    
    XmlNamespaceManager nsmgr = new XmlNamespaceManager(xdoc.NameTable);    
    XmlNodeList nodes = xDoc.SelectNodes("//item[@xsi:type='ns2:Map']",nsmgr);
    var nodeCount=nodes.Count;