无法使用XDocument从XML提取子元素

以下是我要从中提取子元素的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变量将引发“未设置对象 到对象的实例“例外。

谢谢。

hbfym 回答:无法使用XDocument从XML提取子元素

您需要考虑Header的名字节奏。

public static string AssignSecurityToken(string response)
{
    XNamespace ns1 = "http://schemas.xmlsoap.org/soap/envelope/";
    XNamespace ns2 = "http://SomeValue/SomeValue/2009/01/SomeValue.xsd";

    var envelope = XElement.Parse(response);
    var header = envelope.Element(ns1 + "Header");
    var session = header.Element(ns2 + "Session");
    var security_token = session.Element(ns2 + "SecurityToken");

    return security_token.Value;
}

实际上您可以继续拨打电话

return XElement.Parse(response).Descendants()
            .First(x => x.Name.LocalName == "SecurityToken").Value;
,

仅对于 this 响应,仅解析字符串并提取元素是有意义的。 该响应使用两个名称空间,一个用于SOAP标头,另一个用于Amadeus登录响应。您需要第二个来检索令牌:

//SOAP-only namespace
XNamespace soap = "http://schemas.xmlsoap.org/soap/envelope/";
//Default namespace
XNamespace ns = "http://SomeValue/SomeValue/2009/01/SomeValue.xsd";

var response=XElement.Parse(xml);
var token=response.Descendants(ns+"SecurityToken").First().Value;

其他Amadeus响应很大,并且XDocument不会比使用WCF和反序列化强类型对象更好(如果有的话)。 XDocument与DataContractSerializer一样,反序列化整个XML响应。不过,您不必获取一组强类型的对象,而需要将XElement映射到其他对象。

如果要仅通过读取部分来减少内存消耗,则必须使用XmlReader并从响应流中逐一读取XML令牌。还有很多工作。

另一个有趣的事情是Amadeus响应使用多个名称空间。该响应仅使用2。其他响应(例如搜索)使用更多。

,

您可能会考虑使用确实很容易使用的System.Xml.XmlDocumentSystem.Xml.XPath.XPathNavigator

  

我为您编写了一个简单示例(支持UTF-8编码):

System.Xml.XmlDocument someXmlFile = new System.Xml.XmlDocument();
string xmlPath= @"YOUR_XML_FULL_PATH";
string innerNodeToExtract= @"/Session/SecurityToken";

try
{
    // Loading xml file with utf-8 encoding:
    string xmlFileStr= Systm.IO.File.ReadAllText(xmlPath,System.Text.Encoding.UTF8);

    // Creating the XPathNavigator:
    System.Xml.XPath.XPathNavigator xmlNavigator= someXmlFile.CreateNavigator();

    // Getting the inner value as string:
    string value = xmlNavigator.SelectSingleNode(innerNodeToExtract).Value;

    // some code...
}
catch(Exception) 
{
    // Handle failures
}
  

请注意,您还可以:

     
      
  • 使用“ @”键提取内部值。

  •   
  • 使用xmlNavigator.MoveToNext()移动到孩子。

  •   
     

您可以阅读here的许多其他内容。

本文链接:https://www.f2er.com/3158711.html

大家都在问