Sudu
Sudu

Reputation: 51

Read SOAP XML Response in C#

Hi Please check below my Soap XML Response

<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>
<soapenv:Body>
<ns1:getHotelDetail xsi:type='xsd:string' xmlns:ns1='http://axis.frontend.hydra.hotelbeds.com'>
<HotelDetailRS xmlns='http://www.hotelbeds.com/schemas/2005/06/messages' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://www.hotelbeds.com/schemas/2005/06/messages HotelDetailRS.xsd' echoToken='DummyEchoToken'>
<AuditData>
<ProcessTime>15</ProcessTime><Timestamp>2015-03-18 11:30:23.486</Timestamp><RequestHost>54.169.51.224</RequestHost> <ServerName>FORM</ServerName><ServerId>FO</ServerId><SchemaRelease>2005/06</SchemaRelease><HydraCoreRelease>2015.01.14</HydraCoreRelease><HydraEnumerationsRelease>N/A</HydraEnumerationsRelease><MerlinRelease>0</MerlinRelease></AuditData><Hotel xsi:type='ProductHotel'><Code>52319</Code><Name>Intertur Apartamentos Waikiki</Name>

Above is my XML Response to part. But I cannot read this file in C# Please check my C# Cording below

private void GetCustomerList(XmlDocument xml)
{
    //string xmlFilePath = System.Configuration.ConfigurationManager.AppSettings.Get("xmlpathHotel");
    //int i = iRefreshID;
    int icount = 0;
    XmlDocument xdcDocument = new XmlDocument();

    xdcDocument = xml;

    var nsmgr = new XmlNamespaceManager(xdcDocument.NameTable);
    nsmgr.AddNamespace("ns1", "http://axis.frontend.hydra.hotelbeds.com");
    nsmgr.AddNamespace("ns", "http://www.hotelbeds.com/schemas/2005/06/messages");


    var nl = xdcDocument.SelectNodes("/ns:HotelDetailRS/ns:Hotel", nsmgr);

    foreach (XmlNode xndNode in nl)
    {
        Label lbln = new Label();
        lbln.Text = "Code : " + xndNode["Code"].InnerText + "<br />"; ;
        hdetDiv.Controls.Add(lbln);

        Label lblnn = new Label();
        lblnn.Text = "Name : " + xndNode["Name"].InnerText + "<br />"; ;
        hdetDiv.Controls.Add(lblnn);

        Label lblad7 = new Label();
        lblad7.Text = "Category : " + xndNode["Category"].InnerText + "<br />"; ;
        hdetDiv.Controls.Add(lblad7);
    }
}

Xml namespaces that I am setting up fro read XML file is wrong. Can any one help me please.

Upvotes: 0

Views: 3342

Answers (1)

Reinder Wit
Reinder Wit

Reputation: 6615

You could try using an XDocument object (LINQ to XML). It's easier to program against such an object than it is against an XmlDocument.

XDocument xdoc = XDocument.Parse(xdcDocument.OuterXml);
XNamespace ns = "http://www.hotelbeds.com/schemas/2005/06/messages";

foreach (XElement hotel in xdoc.Descendants(ns + "Hotel"))
{
    string code = hotel.Element(ns + "Code").Value;
    string name = hotel.Element(ns + "Name").Value;
}

Upvotes: 3

Related Questions