【发布时间】:2019-11-05 14:43:54
【问题描述】:
以下是我试图从中提取子元素的 XML。
<?xml version="1.0" encoding="UTF8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header xmlns="http://SomeValue/SomeValue/2009/01/SomeValue.xsd">
<Session>
<SessionId>SomeValue</SessionId>
<SequenceNumber>1</SequenceNumber>
<SecurityToken>SomeValue</SecurityToken>
</Session>
</soap:Header>
<soap:Body>
<Security_AuthenticateReply xmlns="http://SomeValue/SomeValue">
<processStatus>
<statusCode>P</statusCode>
</processStatus>
</Security_AuthenticateReply>
</soap:Body>
</soap:Envelope>
public static string AssignSecurityToken(string response)
{
string Token = "";
XNamespace ns = "http://schemas.xmlsoap.org/soap/envelope/";
XElement xdoc = XElement.Parse(response);
XElement root = xdoc.Descendants(ns + "Header").First();
Token = root.Element("Session").Element("SecurityToken").Value;
Token = root.Descendants("Session").Descendants().Where(n => n.Name == "SecurityToken").FirstOrDefault().Value;
return Token;
}
我想提取元素安全令牌。
以下是我已经做过的事情:
尝试使用帖子中建议的方法提取元素 How to get value of child node from XDocument
还发布了一些代码供参考。这两种说法都是 将值分配给 Token 变量正在抛出“未设置对象 到一个对象的实例”异常。
提前致谢。
【问题讨论】:
-
这看起来像是 Amadeus 响应。为什么您尝试自己解析 SOAP 响应而不是使用例如 WCF?
-
你用艾玛迪斯得到了我!好吧,我正在尝试提取安全令牌并将其放入 SOAP 标头中,以发送对企业 API 操作之一的请求……它是从我的旧代码升级到 SOAP 4.0 的一部分。