如何将XML字符串反序列化为实体类对象
1个回答
展开全部
两个错误:
1、xml的根节点是wlb_waybill_search_response,却没有实体类表示
2、XmlSerializer创建的时候用typeof(WaybillApplySubscriptionInfo),序列化却要求List<...>,连个不匹配。
简单的做法就是添加一个实体类:
C# code?
class Test
{
static void Main(string[] args)
{
string xml = ...;
XmlSerializer serializer = new XmlSerializer(typeof(SearchResponse));
SearchResponse searchResponse = (SearchResponse)serializer.Deserialize(new StringReader(xml));
var subInfo= searchResponse.Results;
}
}
[Serializable]
[XmlRoot("wlb_waybill_search_response")]
public class SearchResponse
{
[XmlElement("results")]
public List<WaybillApplySubscriptionInfo> Results;
}
1、xml的根节点是wlb_waybill_search_response,却没有实体类表示
2、XmlSerializer创建的时候用typeof(WaybillApplySubscriptionInfo),序列化却要求List<...>,连个不匹配。
简单的做法就是添加一个实体类:
C# code?
class Test
{
static void Main(string[] args)
{
string xml = ...;
XmlSerializer serializer = new XmlSerializer(typeof(SearchResponse));
SearchResponse searchResponse = (SearchResponse)serializer.Deserialize(new StringReader(xml));
var subInfo= searchResponse.Results;
}
}
[Serializable]
[XmlRoot("wlb_waybill_search_response")]
public class SearchResponse
{
[XmlElement("results")]
public List<WaybillApplySubscriptionInfo> Results;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询