简体   繁体   English

C# 将 HttpResponseMessage 反序列化为对象

[英]C# Deserializing HttpResponseMessage to Object

I'm having some problem deserializeing a HttpResponseMessage into an object.我在将 HttpResponseMessage 反序列化为对象时遇到了一些问题。 The problem is that when the object should have deserialized all fields are null, no exceptions are thrown.问题是,当对象应该反序列化时,所有字段都为空,不会引发异常。

HttpContent content = new StringContent(xml);
content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("text/xml");
HttpResponseMessage response = await client.PostAsync("URL", content).ConfigureAwait(false);
// Parse response
if (response.IsSuccessStatusCode)
{
    XmlSerializer serializer = new XmlSerializer(typeof(ResponseObject));
    Stream responseStream = await response.Content.ReadAsStreamAsync();
    ResponseObject responseObject = serializer.Deserialize(responseStream) as ResponseObject;

    //Possible example of wrong data
    Console.WriteLine(responseObject.Message);
}

[XmlRoot("response")]
public class ResponseObject
{
    [XmlElement("session")]
    public string Session { get; set; }

    [XmlElement("status")]
    public string Status { get; set; }

    [XmlElement("message")]
    public string Message { get; set; }
}

Response message as a string作为字符串的响应消息

"<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>
<response>
    <val n=\"session\">SESSION ID</val>
    <val n=\"status\">201</val>
    <val n=\"message\">Created</val>
</response>"

Have I missed something?我错过了什么吗? I'm pretty new to serializing/deserializing.我对序列化/反序列化很陌生。 Grateful for pointers.感谢指点。

Okay I solved it with the help of Eser and Biscuits in the comments.好的,我在评论中的 Eser 和 Biscuits 的帮助下解决了它。

I was trying to reuse code and did't really think about the response message having a different structure then the earlier project.我试图重用代码,并没有真正考虑与早期项目具有不同结构的响应消息。

I changed my ResponseObject to this:我将我的 ResponseObject 更改为:

[XmlRoot("response")]
public abstract class ResponseObject
{
    [XmlIgnore]
    public bool Success { get; set; }

    [XmlIgnore]
    public string Session
    {
        get
        {
            var result = Values.FirstOrDefault(n => n.Name == "session");
            return result.Value;
        }
    }

    [XmlIgnore]
    public string Status
    {
        get
        {
            var result = Values.FirstOrDefault(n => n.Name == "status");
            return result.Value;
        }
    }

    [XmlIgnore]
    public string Message
    {
        get
        {
            var result = Values.FirstOrDefault(n => n.Name == "message");
            return result.Value;
        }
    }

    [XmlElement("val")]
    public List<ResponseXmlWrapper<string>> Values;
}

public class ResponseXmlWrapper<T>
{
    [XmlAttribute("n")]
    [JsonProperty("n")]
    public string Name { get; set; }

    [XmlText]
    [JsonProperty()]
    public T Value { get; set; }

    public ResponseXmlWrapper()
    {

    }

    public ResponseXmlWrapper(string attributeName, T value)
    {
        Name = attributeName;
        Value = value;
    }
}

I don't know if this is an optimal solution but it works.我不知道这是否是最佳解决方案,但它有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM