C# C XPath Soap,如何导航到嵌入式节点

C# C XPath Soap,如何导航到嵌入式节点,c#,.net,xml,soap,xpath,C#,.net,Xml,Soap,Xpath,在C、Asp.Net中,我试图返回BISearchResponse中的错误节点: 我能够在XMLNode中获取返回的GetWireResult节点。 如何到达错误节点 <?xml version="1.0" encoding="utf-8" ?> - <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSch

在C、Asp.Net中,我试图返回BISearchResponse中的错误节点: 我能够在XMLNode中获取返回的GetWireResult节点。 如何到达错误节点

<?xml version="1.0" encoding="utf-8" ?> 
- <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
-     <soap:Body>
-          <GetWireResponse xmlns="http://OpenSolutions.com/">
                <GetWireResult><?xml version="1.0"?> 
                      <BISearchResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
                            <Error xmlns="https://bixg.choicepoint.com/webservices/3.0"> 
                                   <Message>BI System: Failed to Login</Message> 
                                   <Code>536870917</Code>
                             </Error>
                      </BISearchResponse>  
                </GetWireResult> 
            </GetWireResponse>
       </soap:Body>
  </soap:Envelope>
我的代码: XmlDocument xmlDoc=新的XmlDocument

            xmlDoc.LoadXml(result);

            XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
            nsmgr.AddNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/");
            nsmgr.AddNamespace("ab", "http://OpenSolutions.com/");
            nsmgr.AddNamespace("bg", " https://bixg.choicepoint.com/webservices/3.0");
            nsmgr.AddNamespace("xsd", "http://www.w3.org/2001/XMLSchema");
            nsmgr.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");

            XmlNode xmlnode = xmlDoc.DocumentElement.SelectSingleNode("/soap:Envelope/soap:Body/ab:GetWireResponse", nsmgr);
这对这里有用。 . 我在这里添加xml,但它仅在编辑模式下可见

<?xml version="1.0" encoding="utf-8" ?> 
- <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
- <soap:Body>
- <GetWireResponse xmlns="http://OpenSolutions.com/">
  <GetWireResult><?xml version="1.0"?> <BISearchResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <Error xmlns="https://bixg.choicepoint.com/webservices/3.0"> <Message>BI System: Failed to Login</Message> <Code>536870917</Code> </Error> </BISearchResponse></GetWireResult> 
  </GetWireResponse>
  </soap:Body>
  </soap:Envelope>

你快到了。扩展XPath

"/soap:Envelope/soap:Body/ab:GetWireResponse"

然而,额外的XML序列化卡在中间,使得XML格式不好。我很惊讶它竟然可以被处理。我认为C API应该在xmlDoc.LoadXmlresult上抛出一个异常

另一种方法是使用C代码探索XML文档的结构,并打印出每个节点的子节点,因为上述方法不会为您返回任何信息。例如,如果您获得的是/soap:Envelope/soap:Body/ab:getwiresponse的节点,而不是/soap:Envelope/soap:Body/ab:getwiresponse/ab:getwiresult的节点,那么ab:getwiresponse是否有哪些文本节点子节点,如果是,它们的值和内容是什么?这应该可以让我们深入了解XPath不起作用的原因


如果其中有一块未解析的(即转义的)XML,您可以将其复制出来,并像您所说的那样将其解析为XML,或者使用regexp搜索所需的模式。。。根据复杂性。

在调试模式下,复制此XML时,请尝试选择其他调试可视化工具,例如文本可视化工具。您可以单击datatip中的放大镜图标来选择它

我认为您的XML看起来像:

<?xml version="1.0" encoding="utf-8" ?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <GetWireResponse xmlns="http://OpenSolutions.com/">
      <GetWireResult>
        &lt;?xml version="1.0"?&gt;
        &lt;BISearchResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
        &lt;Error xmlns="https://bixg.choicepoint.com/webservices/3.0">
        &lt;Message>BI System: Failed to Login&lt;/Message&gt;
        &lt;Code>536870917&lt;/Code&gt;
        &lt;/Error&gt;
        &lt;/BISearchResponse&gt;
      </GetWireResult>
    </GetWireResponse>
  </soap:Body>
</soap:Envelope>
没关系。因此,您可以使用以下XPath选择GetWireResult:

/soap:Envelope/soap:Body/ab:GetWireResponse/ab:GetWireResult

然后将其内容加载到新的XML文档中,并获得所需的响应。

我将您的XML示例放入{code}部分。请下次执行此操作,否则XML无法正确显示。在编辑模式下,粘贴XML,然后选择它并按{}按钮。您将在预览中看到代码。然后保存。现在,代码将在编辑模式之外正确显示。如果您将其缩进以便于查看层次结构,这也会有所帮助。我认为,SOAP正文中的内容是编码的XML,因此它不是XML的一部分。既然这是编码的XML,那么一种方法是取消编码它,将其加载到xmlDocument对象并继续解析?不幸的是,当我这样做时,不会返回任何结果that@user:如果问题是里面的东西实际上逃逸了,即使它没有显示出来,在粘贴的表单中,您应该得到/soap:Envelope/soap:Body/ab:getwiresponse/ab:getwiresult的结果,而不是`/soap:Envelope/soap:Body/ab:getwiresponse/ab:getwiresult/ab:BISearchResponse的结果。这就是你看到的吗?实际上,我没有得到ab:GetWireResult或ab:GetWireResult/ab:BISearchRespon的结果‌​东南方。我只得到/soap:Envelope/soap:Body/ab:GetWireResponse@user:请尝试将XML保存到一个文件中,从那里复制它,并将其发布到您的问题中。我假设您从浏览器视图复制了它。返回的xml来自VS 2010中的调试会话,我在var request=HttpWebRequestWebRequest.Createuri;因此,它不是来自浏览器。
<?xml version="1.0" encoding="utf-8" ?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <GetWireResponse xmlns="http://OpenSolutions.com/">
      <GetWireResult>
        <![CDATA[
        <?xml version="1.0"?>
        <BISearchResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
          <Error xmlns="https://bixg.choicepoint.com/webservices/3.0">
            <Message>BI System: Failed to Login</Message>
            <Code>536870917</Code>
          </Error>
        </BISearchResponse>
        ]]>
      </GetWireResult>
    </GetWireResponse>
  </soap:Body>
</soap:Envelope>
/soap:Envelope/soap:Body/ab:GetWireResponse/ab:GetWireResult