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

huangapple go评论63阅读模式
英文:

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

问题

I see you're trying to deserialize an XML response in C# but are facing some issues, especially related to XML namespaces. To resolve the issue, you can update your C# classes and use the XmlNamespace attribute to specify the XML namespace. Here's the modified code:

using System.Xml.Serialization;

namespace TestHarness
{
    [XmlRoot(ElementName = "AssumeRoleResponse", Namespace = "https://sts.amazonaws.com/doc/2011-06-15/")]
    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
    {
        [XmlElement(ElementName = "AccessKeyId")]
        public string AccessKeyId { get; set; }

        [XmlElement(ElementName = "SecretAccessKey")]
        public string SecretAccessKey { get; set; }
    }
}

By specifying the namespace in the XmlRoot and XmlElement attributes, you should be able to deserialize the XML correctly.

英文:

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:

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

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?

答案1

得分: 1

这对我有效:

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#类:

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; }
}

并修复您的XML,需要一个根节点,将< /AssumeRoleResponse >添加到末尾:

...
<ResponseMetadata>
    <RequestId>sdsads</RequestId>
</ResponseMetadata>
</AssumeRoleResponse>
英文:

this works for me

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

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

Result result = JsonConvert.DeserializeObject&lt;Result&gt;(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;

....
&lt;ResponseMetadata&gt;
    &lt;RequestId&gt;sdsads&lt;/RequestId&gt;
&lt;/ResponseMetadata&gt;
&lt;/AssumeRoleResponse&gt;

</details>



huangapple
  • 本文由 发表于 2023年3月21日 00:31:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75792923.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定