2013-05-02 132 views
1

请原谅,我是SOAP和C#的新手。我似乎无法弄清楚如何正确设置命名空间以在SOAP响应中查找节点。解析C#中的SOAP响应......不理解XML命名空间

这里的响应,如果网络服务查询返回空:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> 
    <soapenv:Body> 
     <ns:VXWSResponse xmlns:ns="vx.sx"> 
      <ns:List ns:id="result" /> 
     </ns:VXWSResponse> 
    </soapenv:Body> 
</soapenv:Envelope> 

这里的响应,如果返回的数据:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> 
    <soapenv:Body> 
     <ns:VXWSResponse xmlns:ns="vx.sx"> 
      <ns:List ns:id="result"> 
       <ns:Badge>USER DATA</ns:Badge> 
      </ns:List> 
     </ns:VXWSResponse> 
    </soapenv:Body> 
</soapenv:Envelope> 

我只需要知道,如果该标签存在。

这是我到目前为止。

XmlNamespaceManager manager = new XmlNamespaceManager(xml.NameTable); 
manager.AddNamespace("ns", "vx.sx"); 
manager.AddNamespace("id", "result"); 
xmlNode badge = xml.SelectSingleNode("//id:Badge", manager); 
XmlNode result = xml.SelectSingleNode("//ns:result", manager); 

两个节点都返回null。我已经查看了很多其他文章,但我没有看到如何正确地在响应XML中寻址名称空间。

任何帮助表示赞赏!

+0

你真的需要手动解析SOAP吗?你想达到什么目的? – Dusan 2013-05-02 20:28:54

+0

我无法控制Web服务,因此需要在CE 5/.NET 2上运行,所以我不确定是否可以使用服务引用。我只需要测试标签是否存在。 – alexsd 2013-05-02 20:31:53

回答

1

该id是列表节点的属性,而不是名称空间。

我编辑了我的答案来检查徽章元素,因为这是你似乎想要寻找的。

 XmlNamespaceManager manager = new XmlNamespaceManager(xmlDoc.NameTable); 
     manager.AddNamespace("ns", "vx.sx"); 

     XmlNode badge = xmlDoc.SelectSingleNode("//ns:Badge", manager); 

     if (badge == null) 
     { 
      // no badge element 
     } 
     else 
     { 
      // badge element present 
     } 
+0

它做到了。知道这将是简单的事情。谢谢! – alexsd 2013-05-02 21:24:28

+0

很高兴工作! – Dave 2013-05-02 21:25:21