Read Soap Message using C#
BookHotelResponse
is in the namespace urn:schemas-test:testgate:hotel:2012-06
(the default namespace in the sample xml) so you need to provide that namespace in your queries:
XmlDocument document = new XmlDocument();
document.LoadXml(soapmessage); //loading soap message as string
XmlNamespaceManager manager = new XmlNamespaceManager(document.NameTable);
manager.AddNamespace("d", "http://someURL");
manager.AddNamespace("bhr", "urn:schemas-test:testgate:hotel:2012-06");
XmlNodeList xnList = document.SelectNodes("//bhr:bookHotelResponse", manager);
int nodes = xnList.Count;
foreach (XmlNode xn in xnList)
{
Status = xn["d:bookingStatus"].InnerText;
}
Use LINQ2XML
To read bookingStatus,do this
XElement doc = XElement.Load("yourStream.xml");
XNamespace s = "http://schemas.xmlsoap.org/soap/envelope/";//Envelop namespace s
XNamespace bhr="urn:schemas-test:testgate:hotel:2012-06";//bookHotelResponse namespace
XNamespace d="http://someURL";//d namespace
foreach (var itm in doc.Descendants(s + "Body").Descendants(bhr+"bookHotelResponse"))
{
itm.Element(d+"bookingStatus").Value;//your bookingStatus value
}
LINQ2XML is cool though....:)
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);
}
}