在C#中读取SOAP XML响应

时间:2015-03-18 11:41:59

标签: xml soap

您好请检查我的Soap XML响应

<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>

以上是我对部分的XML响应。但我无法在C#中阅读此文件请查看下面的C#Cording

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文件的Xml命名空间是错误的。任何人都可以帮助我。

1 个答案:

答案 0 :(得分:0)

您可以尝试使用XDocument对象(LINQ to XML)。对这样的对象进行编程比对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;
}