使用 C# 读取 Soap 消息 [英] Read Soap Message using C#

查看:31
本文介绍了使用 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>

我正在尝试使用 C# 阅读上面的肥皂消息 XmlDocument:

I am trying to read the above soap message XmlDocument using 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;
}

计数始终为零,并且不会读取预订状态值.

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

推荐答案

BookHotelResponse 在命名空间 urn:schemas-test:testgate:hotel: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 消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆