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

我正在尝试XmlDocument使用 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;
}

计数始终为零,并且不读取 bookingstatus 值。

4

4 回答 4

16

BookHotelResponse位于命名空间urn:schemas-test:testgate:hotel:2012-06(示例 xml 中的默认命名空间)中,因此您需要在查询中提供该命名空间:

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; 
} 
于 2012-08-30T17:08:47.697 回答
3

利用LINQ2XML

要阅读 bookingStatus,请执行此操作

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 虽然很酷.... :)

于 2012-08-30T17:03:57.080 回答
2

首先,您要创建一个类以将 xml 值反序列化为

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

然后,您可以利用GetElementsByTagName提取肥皂请求的主体并将请求字符串反序列化为对象。

    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);
        }
    }
于 2016-05-01T16:56:29.307 回答
1

据我了解,您希望得到肥皂服务的回复。如果是这样,您不必自己完成所有这些艰苦的工作(拨打电话、解析 xml、选择节点以获取响应值)......相反,您需要将服务引用添加到您的项目中,它将完成所有为您休息工作,包括生成类、进行 asmx 调用等...在此处阅读更多相关信息https://msdn.microsoft.com/en-us/library/bb628649.aspx

添加引用后,您需要做的就是调用类似这样的类方法

var latestRates = (new GateSoapClient())?.ExchangeRatesLatest();
return latestRates?.Rates;
于 2018-03-26T07:45:11.530 回答