gpt4 book ai didi

c# - 在 C# 中从 SOAP 信封中提取 XML

转载 作者:行者123 更新时间:2023-11-30 22:24:20 25 4
gpt4 key购买 nike

我在 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# 获取值的步骤。

最佳答案

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;

关于c# - 在 C# 中从 SOAP 信封中提取 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12818248/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com