Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read Soap Message using C#

Tags:

c#

soap

xml

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Head>
    <h:talkId s:mustknow="1" xmlns:h="urn:schemas-test:testgate:hotel:2012-06">
      sfasfasfasfsfsf</h:talkId>
    </s:Head>
  <s:Body>
    <bookHotelResponse xmlns="urn:schemas-test:testgate:hotel:2012-06" xmlns:d="http://someURL" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
      <d:bookingReference>123456</d:bookingReference>
      <d:bookingStatus>successful</d:bookingStatus>
      <d:price xmlns:p="moreURL">
        <d:total>105</d:total>
      </d:price>
    </bookHotelResponse>
  </s:Body>
</s:Envelope>

I am trying to read the above soap message XmlDocument using C#:

XmlDocument document = new XmlDocument();
document.LoadXml(soapmessage);  //loading soap message as string
XmlNamespaceManager manager = new XmlNamespaceManager(document.NameTable);

manager.AddNamespace("d", "http://someURL");

XmlNodeList xnList = document.SelectNodes("//bookHotelResponse", manager);
int nodes = xnList.Count;

foreach (XmlNode xn in xnList)
{
    Status = xn["d:bookingStatus"].InnerText;
}

The count is always zero and it is not reading the bookingstatus values.

like image 472
delwasaf ewrew Avatar asked Aug 30 '12 16:08

delwasaf ewrew


People also ask

What is SOAP C#?

SOAP stands for Simple Object Access Protocol.

What is a SOAP message?

A SOAP message is an XML document that consists of a SOAP envelope, an optional SOAP header, and a SOAP body. The SOAP message header contains information that allows the message to be routed through one or more intermediate nodes before it reaches its final destination.

What is the message format of SOAP protocol?

A SOAP message is encoded as an XML document, consisting of an <Envelope> element, which contains an optional <Header> element, and a mandatory <Body> element. The <Fault> element, contained in <Body> , is used for reporting errors.

What is SOAP with example?

SOAP is the Simple Object Access Protocol, a messaging standard defined by the World Wide Web Consortium and its member editors. SOAP uses an XML data format to declare its request and response messages, relying on XML Schema and other technologies to enforce the structure of its payloads.


2 Answers

As I understand you want to get response from soap service. If so, you don't have to do all this hard work (making call, parsing xml, selecting nodes to get the response value) by yourself... instead you need to Add Service Reference to your project and it will do all the rest work for you, including generating class, making asmx call and so on... Read more about it here https://msdn.microsoft.com/en-us/library/bb628649.aspx

Everything you'll need to do after adding reference is to invoke a class method something like this

var latestRates = (new GateSoapClient())?.ExchangeRatesLatest();
return latestRates?.Rates;
like image 66
GuChil Avatar answered Oct 07 '22 09:10

GuChil


First you want to create a class to deseralize the xml values into

    public class bookHotelResponse {
      public int bookingReference { get; set; }
      public int bookingStatus { get; set; }
   } 

Then you can utilize GetElementsByTagName to extract the body of the soap request and deseralize the request string into an object.

    private static T DeserializeInnerSoapObject<T>(string soapResponse)
    {
        XmlDocument xmlDocument = new XmlDocument();
        xmlDocument.LoadXml(soapResponse);

        var soapBody = xmlDocument.GetElementsByTagName("soap:Body")[0];
        string innerObject = soapBody.InnerXml;

        XmlSerializer deserializer = new XmlSerializer(typeof(T));

        using (StringReader reader = new StringReader(innerObject))
        {
            return (T)deserializer.Deserialize(reader);
        }
    }
like image 22
Rich Hildebrand Avatar answered Oct 07 '22 10:10

Rich Hildebrand