我尝试接收XML格式的HTTP POST Reponse,并使用类
包含List这是来自服务器的XML:
<list_bulk_response>
<status_code>1100</status_code>
<status_text>OK</status_text>
<list_request>
<request_bulk>
<request_status_code>1100</request_status_code>
<request_status_text>OK</request_status_text>
<id>1</id>
</request_bulk>
<request_bulk>
<request_status_code>1100</request_status_code>
<request_status_text>OK</request_status_text>
<id>2</id>
</request_bulk>
....
....
....
</list_request>
</list_bulk_response>
在许多来源搜索后,我试试这个:
public class list_bulk_response :
{
public string status_code { get; set; }
public string status_text { get; set; }
public list_request list_request { get; set; }
}
public class list_request
{
public List<request_bulk> request_bulk { get; set; }
}
public class request_bulk
{
public string request_status_code { get; set; }
public string request_status_text { get; set; }
public string id{ get; set; }
}
和此:
public class list_bulk_response : KenjaloApi
{
public List<list_request> list_request { get; set; }
}
public class list_request
{
public request_bulk request_bulk { get; set; }
}
public class request_bulk
{
public string request_status_code { get; set; }
public string request_status_text { get; set; }
public string request_date { get; set; }
public string transaction_id { get; set; }
public string priority { get; set; }
public string sender { get; set; }
public string dr_url { get; set; }
}
但仍无法收到XML内的列表
更新 这是我的反序列化代码:
public static T deserializeXMLtoClass<T>(string input) where T : class
{
System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(typeof(T));
using (StringReader sr = new StringReader(input))
{
return (T)ser.Deserialize(sr);
sr.Close();
}
}
班上有什么不对吗?
答案 0 :(得分:2)
你的第一个模型几乎是正确的。您只是缺少一个简单的提示 [XmlElement("request_bulk")]
var t = deserializeXMLtoClass<list_bulk_response>(xmlstring);
public class list_bulk_response
{
public string status_code { get; set; }
public string status_text { get; set; }
public list_request list_request { get; set; }
}
public class list_request
{
[XmlElement("request_bulk")]
public List<request_bulk> request_bulk { get; set; }
}
public class request_bulk
{
public string request_status_code { get; set; }
public string request_status_text { get; set; }
public string id { get; set; }
}
答案 1 :(得分:0)
你可以做这样的事情
using (Stream httpResponseStream = response.Content.ReadAsStreamAsync().Result)
{
XDocument responseXML = XDocument.Load(httpResponseStream);
IEnumerable<XElement> listBulkResponseElements = responseXML.Root.
Elements("list_bulk_response");
foreach (XElement lbre in listBulkResponseElements)
{
list_bulk_response lbr = new list_bulk_response();
request_bulk rb = new request_bulk();
// Access the values with Element(valueName).value like this
lbr.statuscode = lbre.Element("statuscode").value;
lbr.statustext = lbre.Element("statustext").value;
// For nested elements you need to add extra Element(valueName)s like this
rb.request_status_code = Root.Element("list_request").Elements("request_bulk").
Element("request_status_code").value;
// Notice that "Element" is used where you only have one of them like with your
// "list_bulk_response" and "Elements" is used where you have multiple ones
// like with "request_bulk"
// And so on
// Now you need to decide what do do with your objects
}
}