kalls
kalls

Reputation: 2855

Linq to XML - how to get the value of a attribute

`

XElement config = XElement.Parse (
@"<Response SessionId='426D9AEB1F684849A16D79A6CF48582B' xmlns='http://schemas.tmaresources.com/timssws60.xsd'>
<Status Success='true' Message='Connected' ErrorCode='0' />
</Response>");

XElement response = config.Element("Response");

sessionID = (string)response.Attribute("SessionId");`

why is response null in this case? how can I get the attribute value SessionId?

Upvotes: 1

Views: 172

Answers (1)

SLaks
SLaks

Reputation: 887453

Your config variable contains the <Response> element itself.
Calling config.Element("Response") will try to get a <Response> element inside the <Response> element.
Since there isn't any, it returns null.

Change it to

(string)config.Attribute("SessionId")

Upvotes: 1

Related Questions