简体   繁体   English

在asp.net的Xdocument中从Complex Xml获取特定标记值

[英]Get Specific Tag value From Complex Xml in Xdocument in asp.net

As I am a Beginner i Certainly need your Help. 作为初学者,我当然需要您的帮助。 I want to get a Specific Tag from Xdocument. 我想从Xdocument获取特定标签。 Following is the Content in the Xdocument: 以下是Xdocument中的内容:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <UcicLoginResponse xmlns="http://tempuri.org/">
      <UcicLoginResult>
        <Success>true</Success>
        <authToken>xxxxxxx</authToken>
      </UcicLoginResult>
    </UcicLoginResponse>
  </soap:Body>
</soap:Envelope>

Then I want to get the Value of the tag authToken. 然后,我想获取标记authToken的值。 Tried Lot with Descentants and Elements..But,due to the xml attributes,All tries leds to error.Anyone pls Help me... 尝试使用Descentant和Elements ..但是,由于xml属性,所有尝试都导致错误。任何人都可以帮助我...

Some of my tries Given: 我的一些尝试:

 XDocument _Xresult = XDocument.Parse(XmlResponse.Elements().Single().Value);
 IEnumerable<XElement> xResponseItem = _Xresult.Descendants("UcicLoginResult");
                if (xResponseItem.Descendants("Remarks").Any())
                {
                    string sErr = _Xresult.Element("Remarks").Value;
                    throw new Exception("Authentication failed : " + sErr);
                }
                token = _Xresult.Descendants("authToken").FirstOrDefault().Value;
#
IEnumerable<XElement> xResponseItem =XmlResponse.Descendants("UcicLoginResponse");

string sErr = xResponseItem.Descendants("UcicLoginResult").FirstOrDefault().Element("authToken").Value;
#
 var resp=XmlResponse.Descendants("soap:Envelope").Descendants("soap:Body").Descendants("UcicLoginResponse").Descendants("UcicLoginResult").Elements("authToken"); 
#
 IEnumerable<XElement> xResponseItem =XmlResponse.Descendants("UcicLoginResponse"); string sErr = xResponseItem.Descendants("UcicLoginResult").FirstOrDefault().Element("authToken").Value; 
#
 var res = XmlResponse.Descendants("soap:Envelope").Descendants("soap:Body").Descendants("UcicLoginResponse").Descendants("UcicLoginResult").Elements("authToken"); 

You are not specifying the namespace. 您没有指定名称空间。 http://tempuri.org/ ; http://tempuri.org/ ;

var xDocument = XDocument.Parse(xml);
XNamespace ns = "http://tempuri.org/";
var authToken = xDocument.Descendants(ns + "authToken").FirstOrDefault();

I don't know the xml where is it come from but it seems that you are communicating with a SOAP service and it could be better to get the data as object based by WCF client. 我不知道xml的来源,但似乎您正在与SOAP服务进行通信,因此通过WCF客户端将数据作为对象获取可能会更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM