使用C#读取Soap消息

时间:2012-08-30 16:53:37

标签: c# xml 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
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 个答案:

答案 0 :(得分: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; 
} 

答案 1 :(得分: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 虽然......:)

答案 2 :(得分:1)

首先,您要创建一个类以将xml值解除分区为

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

然后,您可以利用GetElementsByTagName提取soap请求的正文,并将请求字符串解析为对象。

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

答案 3 :(得分:0)

据我了解,您希望得到肥皂服务的回复。如果是这样,你不必自己完成所有这些艰苦的工作(打电话,解析xml,选择节点以获得响应值)......相反,你需要为你的项目添加服务参考,它会做所有其他工作适合你,包括生成课程,制作asmx电话等等...... 请在此处详细了解https://msdn.microsoft.com/en-us/library/bb628649.aspx

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

var latestRates = (new GateSoapClient())?.ExchangeRatesLatest();
return latestRates?.Rates;