开发者

C# XPath Soap, how to navigate to embedded node

In C#, Asp.Net, I am trying to return the Error node inside of BISearchResponse: I am able to get the GetWireResult node returned in an XMLNode. How do I get to the Error node?

<?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>

My code: XmlDocument xmlDoc = new 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);

This works to here. . I am adding the xml here, but it is only visible in edit mode.

<?xml version="1.0" encoding="utf-8" ?> 
- <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/env开发者_StackOverflowelope/" 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>


In debug mode, when you copy this XML, try choose another debug visualizer, e.g. "Text visualizer". You can select it clicking the magnifying glass icon in datatip.

I think your XML looks like:

<?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>

or

<?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>

No matter. So you can select GetWireResult using following XPath:

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

and then load it content in new XML document and get desired response.


You're almost there. Extend your XPath

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

to

"/soap:Envelope/soap:Body/ab:GetWireResponse/ab:GetWireResult/ab:BISearchResponse/bg:Error"

However that extra XML prologue stuck in there in the middle, <?xml version="1.0"?>, makes the XML not well-formed. I'm surprised it can be processed at all. I would think the C# API should throw an exception on xmlDoc.LoadXml(result).

Another approach, seeing as the above does not return anything for you, would be to use your C# code to explore the structure of the XML document and print out the children of each node. E.g. if you are getting a node for "/soap:Envelope/soap:Body/ab:GetWireResponse" but not for "/soap:Envelope/soap:Body/ab:GetWireResponse/ab:GetWireResult", does ab:GetWireResponse have what text node children, and if so, what are their values (contents)? That should give insight into why the XPath is not working.

If there's a block of unparsed (i.e. escaped) XML in there, you could either copy it out and parse it as XML like you said, or just search for the pattern you need using a regexp... depending on the complexity.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜