简体   繁体   中英

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. Following is the Content in the 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. Tried Lot with Descentants and Elements..But,due to the xml attributes,All tries leds to error.Anyone pls Help me...

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/ ;

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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