I have the following xml string:
<AssumeRoleWithWebIdentityResponse xmlns="https://sts.amazonaws.com/doc/2011-06-15/">
<AssumeRoleWithWebIdentityResult>
<SubjectFromWebIdentityToken>us-east-1:xxxx</SubjectFromWebIdentityToken>
<Audience>us-east-1:xxxxxxxx</Audience>
<AssumedRoleUser>
<Arn>arn:aws:sts::xxxxxx/xxxxxx</Arn>
<AssumedRoleId>xxxx</AssumedRoleId>
</AssumedRoleUser>
<Credentials>
<SessionToken>xxxxxx</SessionToken>
<SecretAccessKey>xxxxx</SecretAccessKey>
<Expiration>2015-02-03T15:26:03Z</Expiration>
<AccessKeyId>xxxxxxx</AccessKeyId>
</Credentials>
<Provider>xxxxx</Provider>
</AssumeRoleWithWebIdentityResult>
<ResponseMetadata>
<RequestId>xxxxxx</RequestId>
</ResponseMetadata>
</AssumeRoleWithWebIdentityResponse>
I would like to retrieve the SessionToken within the <Credentials>
node
I tried the following:
XDocument xdoc = XDocument.Parse(response);
var root = xdoc.Root.Element("AssumeRoleWithWebIdentityResponse");
but the variable root is returning null.
How do I retrieve the SessionToken from the <Credentials>
node?
You've made two mistakes:
AssumeRoleWithWebIdentityResponse
element under the root element; it is the root elementAssumeRoleWithWebIdentityResponse
element is in a namespace (https://sts.amazonaws.com/doc/2011-06-15/"
) so you need to specify that when you call Element
or anything similar. All the descendant elements inherit that namespace too, unless they specify something different.So for example:
XNamespace ns = https://sts.amazonaws.com/doc/2011-06-15/";
var root = xdoc.Root;
var result = root.Element(ns + "AssumeRoleWithWebIdentityResult");
To get the SessionToken
from the Credentials
tag, you could either be very specific:
var token = root.Element(ns + "AssumeRoleWithWebIdentityResult")
.Element(ns + "Credentials")
.Element(ns + "SessionToken")
.Value;
Or just use Descendants
:
var token = root.Descendants(ns + "SessionToken").Single().Value;
Either way, it's using the namespace which is the important part.
See more on this question at Stackoverflow