Reputation: 279
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;
#
var root = XmlResponse.Root;
var res1= root.Elements("UcicLoginResult").Elements("authToken").FirstOrDefault().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");
Upvotes: 2
Views: 758
Reputation: 13146
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.
Upvotes: 3