-1

I am getting the following xml response from the API endpoint:

<AssumeRoleResponse xmlns="https://sts.amazonaws.com/doc/2011-06-15/">
    <AssumeRoleResult>
        <AssumedRoleUser>
            <AssumedRoleId>Aasd</AssumedRoleId>
            <Arn>aasddsd23</Arn>
        </AssumedRoleUser>
        <Credentials>
            <AccessKeyId>asd</AccessKeyId>
            <SecretAccessKey>asda</SecretAccessKey>
            <SessionToken>asdasdad</SessionToken>
            <Expiration>2023-03-20T16:13:27Z</Expiration>
        </Credentials>
    </AssumeRoleResult>
    <ResponseMetadata>
        <RequestId>sdsads</RequestId>
    </ResponseMetadata>
</AssumeRoleResponse>

My target is deserialize this xml http content to an object.

The object I have is this:

namespace TestHarness
{
    [XmlRoot(ElementName = "AssumeRoleResponse")]
    public class AssumeRoleResponse
    {
        [XmlElement(ElementName = "AssumeRoleResult")]
        public AssumeRoleResult AssumeRoleResult { get; set; }
    }

    public class AssumeRoleResult
    {
        [XmlElement(ElementName = "Credentials")]
        public Credentialss Credentialss { get; set; }
    }

    public class Credentialss
    {
        [XmlAttribute("AccessKeyId")]
        public string AccessKeyId { get; set; }

        [XmlAttribute("SecretAccessKey")]
        public string SecretAccessKey { get; set; }
    }
}

And the code I am using for deserializing xml is following:

var signedRequest = client.GetAsync(endpoint);

var response = await signedRequest;
var content = await response.Content.ReadAsStringAsync();
XmlSerializer xmls = new XmlSerializer(typeof(AssumeRoleResponse));
var assignmentResult = (AssumeRoleResponse)xmls.Deserialize(new StringReader(content));

but given code throwing me following error:

enter image description here

I have found that if I remove this xmlns="https://sts.amazonaws.com/doc/2011-06-15/ line from xml response it does not crush but still the Credentialss object property values null. Anyone has any idea what exactly I am missing?

Sepehr
  • 13
  • 5
  • 1
    Please [do not post images of code/data/errors](https://idownvotedbecau.se/imageofcode) – Stu Mar 20 '23 at 16:33
  • okay sorry I am editing my question – Fazla Elahi Md Jubayer Mar 20 '23 at 16:34
  • @Stu I have updated my post, please have a look – Fazla Elahi Md Jubayer Mar 20 '23 at 16:37
  • The error says xml is bad at (1,2) which is line one character two. If you are getting a response than credentials are good except if the status is bad. Normally a HTTP response will have a status of 200 OK and a 400/500 error. If you are getting a 200 OK than credentials are ok and the XML response is not formatted correctly. – jdweng Mar 20 '23 at 16:41
  • Does this answer your question? [How can I XML Serializable namespace prefixes?](https://stackoverflow.com/questions/28269949/how-can-i-xml-serializable-namespace-prefixes) – Charlieface Mar 20 '23 at 16:54
  • Side note: for performance you can do `ReadAsStreamAsync` and pass that straight to a `StreamReader` and on to `XmlSerializer`. But harder to debug though – Charlieface Mar 20 '23 at 16:55

1 Answers1

1

this works for me

var content = await response.Content.ReadAsStringAsync();

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(content);

Result result = JsonConvert.DeserializeObject<Result>(JsonConvert.SerializeXmlNode(xmlDoc, Newtonsoft.Json.Formatting.None,true)).Dump();

c# classes

public class Result
{
    public AssumeRoleResult AssumeRoleResult { get; set; }
    public ResponseMetadata ResponseMetadata { get; set; }
}

public class AssumedRoleUser
{
    public string AssumedRoleId { get; set; }
    public string Arn { get; set; }
}

public class AssumeRoleResult
{
    public AssumedRoleUser AssumedRoleUser { get; set; }
    public Credentials Credentials { get; set; }
}

public class Credentials
{
    public string AccessKeyId { get; set; }
    public string SecretAccessKey { get; set; }
    public string SessionToken { get; set; }
    public DateTime Expiration { get; set; }
}

public class ResponseMetadata
{
    public string RequestId { get; set; }
}

and fix your xml, it needs a root, add < /AssumeRoleResponse > to end;

....
<ResponseMetadata>
    <RequestId>sdsads</RequestId>
</ResponseMetadata>
</AssumeRoleResponse>
Serge
  • 40,935
  • 4
  • 18
  • 45