本文介绍了使用C#阅读SOAP消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我想读上面的SOAP消息的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值

The count is always zero and it is not reading the bookingstatus values.

推荐答案

BookHotelResponse 是命名空间中的瓮:架构 - 测试:testgate:酒店:2012-06 (样品XML默认命名空间),所以你需要提供该命名空间中查询:

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

这篇关于使用C#阅读SOAP消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 18:32