【问题标题】:Read SOAP XML Response in C#在 C# 中读取 SOAP XML 响应
【发布时间】:2015-03-18 20:11:36
【问题描述】:

您好,请查看我的 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 命名空间是错误的。谁能帮帮我。

【问题讨论】:

    标签: xml soap


    【解决方案1】:

    您可以尝试使用 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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-30
      • 1970-01-01
      • 2013-08-04
      • 2021-06-07
      • 1970-01-01
      • 2016-01-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多