使用 C# 读取 Soap 消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12201822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 22:27:04  来源:igfitidea点击:

Read Soap Message using C#

c#xmlsoap

提问by delwasaf ewrew

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

I am trying to read the above soap message XmlDocumentusing C#:

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

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

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

采纳答案by dkackman

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

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

回答by Anirudha

Use LINQ2XML

LINQ2XML

To read bookingStatus,do this

要阅读预订状态,请执行此操作

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 is coolthough....:)

不过 LINQ2XML很酷....:)

回答by Rich Hildebrand

First you want to create a class to deseralize the xml values into

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

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

Then you can utilize GetElementsByTagNameto extract the body of the soap request and deseralize the request string into an object.

然后你可以利用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);
        }
    }

回答by GuChil

As I understand you want to get response from soap service. If so, you don't have to do all this hard work (making call, parsing xml, selecting nodes to get the response value) by yourself... instead you need to Add Service Reference to your project and it will do all the rest work for you, including generating class, making asmx call and so on... Read more about it here https://msdn.microsoft.com/en-us/library/bb628649.aspx

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

Everything you'll need to do after adding reference is to invoke a class method something like this

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

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