Search code examples
c#.net-corexmlserializerxml-deserialization

Cannot able to deserialize HttpResponseMessage from XML to an Object in c#


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?


Solution

  • 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>