Search code examples
c#soapresponse

Parsing a SOAP Response with C#


I've been trying to use the data from an API but I have not been able to read the XML Response from it.

It cames in the form:

    <?xml version="1.0" standalone="no"?>
        <SOAP-ENV:Envelope xmlns:SOAPSDK1="http://www.w3.org/2001/XMLSchema" xmlns:SOAPSDK2="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAPSDK3="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
        <SOAP-ENV:Body SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
            <SOAPSDK4:GetStoreProductsResponse xmlns:SOAPSDK4="http://www.externalwebservice.com/message/">
                <StoreProducts>
                    <StoreID></StoreID>
                    <Products></Products>
                </StoreProducts>
            </SOAPSDK4:GetStoreProductsResponse></SOAP-ENV:Body>
        </SOAP-ENV:Envelope>

And what I need is what is inside Products (for now).

I was trying to use Using C# to parse a SOAP Response (and others to not flood this) without results.

My code:

    XDocument tst = XDocument.Load("Response.xml");
    XNamespace xmlns = "http://schemas.xmlsoap.org/soap/envelope/";
    var tstr = from result in tst.Descendants(xmlns + "StoreProducts") select result.Element("Products").Value;

I am almost sure that I am missing something basic.

Any clue will be really appreciated.

Thank you.


Solution

  • In your XML StoreProducts is not within an XML namespace, just do :

    var tstr = from result in tst.Descendants("StoreProducts") 
               select result.Element("Products").Value;
    

    The example code you gave would have been successful if the inner XML looked like this:

      <SOAP-ENV:StoreProducts>
        <StoreID></StoreID>
        <Products></Products>
      </SOAP-ENV:StoreProducts>