2

我在 SOAP 信封中收到来自 Web 服务的响应,如下所示:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
  <ProcessTranResponse xmlns="http://www.polaris.co.uk/XRTEService/2009/03/">
    <ProcessTranResult xmlns:a="http://schemas.datacontract.org/2004/07/XRTEService" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
      <a:PrintFormFileNameContents i:nil="true"/>
      <a:ResponseXML>response_message</a:ResponseXML>
    </ProcessTranResult>
  </ProcessTranResponse>
</s:Body>

我想得到response_message一个字符串变量。我试着做

XDocument doc = XDocument.Parse(Response);
XNamespace xmlnsa = "http://schemas.datacontract.org/2004/07/XRTEService";
var ResponseXML = doc.Descendants(xmlnsa + "ResponseXML");

当我使用 watch 时,我在ResponseXML -> Results View[0] -> Value我的 中看到response_message,但我无法弄清楚从 C# 获取 Value 的下一步是什么。

4

2 回答 2

2

XContainer.Descendants返回元素的集合。然后你应该尝试这样的事情:

foreach (XElement el in ResponseXML)
{
    Console.WriteLine(el.Value);
}

或者,如果您知道始终只有一个响应,则可以执行以下操作:

XDocument doc = XDocument.Parse(Response);

XNamespace xmlnsa = "http://schemas.datacontract.org/2004/07/XRTEService";

XElement ResponseXML = (from xml in XMLDoc.Descendants(xmlnsa + "ResponseXML")
                        select xml).FirstOrDefault();

string ResponseAsString = ResponseXML.Value;
于 2012-10-10T11:38:26.157 回答
1

您可以采用多种解决方案来满足您的目的,而您可能想介绍或不介绍 xml 内容的结构。

静态姿态

你可以简单地使用这个:

XmlDocument _doc = new XmlDocument();
doc.LoadXml(_stream.ReadToEnd());

然后找到所需的数据,如下所示:

doc.LastChild.FirstChild.FirstChild.LastChild.InnerText;

读取xml结构

您可以编写一些额外的代码行来引入命名空间和其他 xml 内容以查找/映射可用数据,方法是查看extracting-data-from-a-complex-xml-with-linq

于 2015-05-30T09:25:13.907 回答