C# 将 HttpResponseMessage 反序列化为对象
C# Deserializing HttpResponseMessage to Object
我在将 HttpResponseMessage 反序列化为对象时遇到了一些问题。
问题是,当对象应该反序列化时,所有字段都为空,不会抛出任何异常。
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; }
}
响应消息为字符串
"<?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>"
我错过了什么吗?我是 serializing/deserializing 的新手。
多谢指点
好的,我在评论中的 Eser 和 Biscuits 的帮助下解决了它。
我试图重用代码,并没有真正考虑响应消息与早期项目具有不同的结构。
我将 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;
}
}
我不知道这是否是最佳解决方案,但它有效。
我在将 HttpResponseMessage 反序列化为对象时遇到了一些问题。 问题是,当对象应该反序列化时,所有字段都为空,不会抛出任何异常。
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; }
}
响应消息为字符串
"<?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>"
我错过了什么吗?我是 serializing/deserializing 的新手。 多谢指点
好的,我在评论中的 Eser 和 Biscuits 的帮助下解决了它。
我试图重用代码,并没有真正考虑响应消息与早期项目具有不同的结构。
我将 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;
}
}
我不知道这是否是最佳解决方案,但它有效。