Link to home
Start Free TrialLog in
Avatar of ts84zs
ts84zs

asked on

Read SOAP Response - XDocument Class in C#

I request SOAP using HTTPWebRequest.

My soap Response looks like this -
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
  <soapenv:Body>
    <SRCData xmlns="http://oracle.com/SRCData.xsd" dateTimeTagFormat="xsd:strict">
      <request>
        <PrimaryKeyID />         
        <AddressID/>
      </request>
      <response>
        <messageData>
          <messageCategory>100</messageCategory>
          <messageNumber>10</messageNumber>
          <errorMessage>No Error</errorMessage>        
          <messageNumberUpdate>10</messageNumberUpdate>         
        </messageData>
      </response
    </SRCData>
  </soapenv:Body>
</soapenv:Envelope>   

Open in new window


I store it in XDocument Variable as shown below... I am trying to read <response> object, <messageData> etc, but it did not work...
 But it does not work -
 
using (WebResponse response = request.GetResponse())
            {
                using (StreamReader rd = new StreamReader(response.GetResponseStream()))
                {
                    XDocument mydoc = XDocument.Load(rd);
                    
                    XNamespace ns = mydoc.Root.GetDefaultNamespace();

                    var pricres = from o in mydoc.Root.Elements(ns + "response").Elements(ns + "response")
                                  select (int)o.Element(ns + "messageData");
                    
                  
                }
            }

Open in new window

Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Hi ts84zs;

See if doing it like this gives you what you need.
using (WebResponse response = request.GetResponse())
{
    using (StreamReader rd = new StreamReader(response.GetResponseStream()))
    {
        XDocument mydoc = XDocument.Load(rd);
        
        XNamespace ns = "http://oracle.com/SRCData.xsd";
        
        var pricres = (from o in mydoc.Root.Descendants(ns + "messageData")
                       select new
                       {
                           MessageCategory = (int)o.Element(ns + "messageCategory"),
                           MessageNumber = (int)o.Element(ns + "messageNumber"),
                           ErrorMessage = o.Element(ns + "errorMessage").Value,
                           MessageNumberUpdate = (int)o.Element(ns + "messageNumberUpdate")
                       }).FirstOrDefault();
        
        Console.WriteLine("MessageCategory = {0}\nMessageNumber = {1}\nErrorMessage = {2}\nMessageNumberUpdate = {3}",
                           pricres.MessageCategory, pricres.MessageNumber, pricres.ErrorMessage, pricres.MessageNumberUpdate);
    }
}

Open in new window

Avatar of ts84zs
ts84zs

ASKER

Thanks
Let me try it
Avatar of ts84zs

ASKER

is there any reference or tutorial i can refer to - how to read a soap xml?

thanks so much
I am sorry I do not know of any. Did the solution I posted work for you?
Avatar of ts84zs

ASKER

yes that worked.. after i used the namespace...
thanks so much...

are there any points I should consider while reading soap-xml... pl guide

thanks again...
Avatar of ts84zs

ASKER

i am trying to find how to read all the elements of that soap-xml
may be using linq
but i am finding difficult reading my xml

thanks again
Avatar of ts84zs

ASKER

is xmltextreader class better than xdocument
pl guide
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ts84zs

ASKER

Ok thanks so much
I think I found my answer
I will close solution soon
Thanks
Still having issues or do you have it?