如何将该XML字符串序列化/反序列化为C#对象?
<Response>
<Business Number="99696" Name="My business" Address="" />
<Purchase PurchaseID="7" CustomerID="0" >
<Item Name="item 1" Qty="100" UnitCost="10.0000" />
<Item Name="item2" Qty="200" UnitCost="20.0000" />
</Purchase>
</Response>
谢谢rubenc
我认为这是一个不同的问题,因为所有问题通常都涉及一个级别或一个项目列表
我的问题是我有不同的水平:
<Response> //root
<Business ... /> //level 1
<Purchase... > //level 1
<Item ... /> // list
我的电话号码,姓名等都为空。
到目前为止,这是我尝试过的:
[Serializable, XmlRoot("Response")]
public class Response
{
public Depot depot = new Depot();
// I have tried this also:
//[XmlElement("Number")]
//public string Number { get; set; }
//[XmlElement("Name")]
//public string Name { get; set; }
}
public class Depot
{
[XmlElement("Number")]
public string Number { get; set; }
[XmlElement("Name")]
public string Name { get; set; }
}
static object DeserializeResponse(string responseString)
{
var serializer = new XmlSerializer(typeof(Response));
Response result;
using (var reader = new StringReader(responseString))
{
result = (Response)serializer.Deserialize(reader);
}
return result;
}
慕姐4208626
相关分类