2
votes

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/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>
2
I put your XML sample into a {code} section. Please do this next time, or the XML doesn't show up (properly). - LarsH
In edit mode, paste in your XML, then select it and press the "{}" button. You'll see the code show up in the preview. Then save. The code will now display correctly outside of edit mode. - LarsH
It would help too if you would indent it so that the hierarchical structure is easy to see. - LarsH
I think, content inside SOAP body <GetWireResult> is encoded XML, so it isn't part of XML. - Kirill Polishchuk
since that is encoded xml, would one approach be to unencode it, load it into an xmlDocument object and continue parsing? - user825374

2 Answers

1
votes

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.

0
votes

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.