C# 序列化反序列化复杂多层xml(webservice接口)
注意,写在开头!
也就是说我们在使用XmlSerializer序列化,初始化XmlSerializer对象时最好使用下面两个构造函数否则会引起内存泄漏。
XmlSerializer(Type)
XmlSerializer.XmlSerializer(Type, String)
下面是一个我们从服务器上接口获得的xml。
<?xml version="1.0" encoding="UTF-8"?> <DataDocument> <DataHead> <DataVersion>1.0.0.0</DataVersion> <DataType>PurchaseSubOrderList</DataType> <DataPageCount>1</DataPageCount> <DataPageNumber>2</DataPageNumber> </DataHead> <DataBody> <PurchaseSubOrderList> <PurchaseSubOrder> <PurchaseSubOrderHead> <OrderCode>3</OrderCode> <OrderType>4</OrderType> <OrderLevel>5</OrderLevel> <OrganizationCode>6</OrganizationCode > <OrganizationName>7</OrganizationName > <DepartmentCode>8</DepartmentCode> <DepartmentName>9</DepartmentName> <DepartmentAddress>10</DepartmentAddress> <DeliveryType>11</DeliveryType> <SupplierCode>12</SupplierCode> <SupplierName>13</SupplierName> <OrderRemark>14</OrderRemark> <OrderDate>15</OrderDate> <OrderAmount>16</OrderAmount> <ListCount>17</ListCount> </PurchaseSubOrderHead> <PurchaseSubOrderItemList> <PurchaseSubOrderItem> <DrugCode>xxxxxxx</DrugCode> <PurchaseCount>xx</PurchaseCount> <UnitPrice>xx .xxxx</UnitPrice> <MinUnit>xx</MinUnit> <UNIT></UNIT> <CargoSpace>xx</CargoSpace> <PurchaseMoney> xx.xxxx</PurchaseMoney > <PurchaseRemark></PurchaseRemark> <ExtendFiled></ExtendFiled> </PurchaseSubOrderItem> <PurchaseSubOrderItem> <DrugCode>xxxxxxx</DrugCode> <PurchaseCount>xx</PurchaseCount> <UnitPrice>xx .xxxx</UnitPrice> <MinUnit>xx</MinUnit> <UNIT></UNIT> <CargoSpace>xx</CargoSpace> <PurchaseMoney> xx.xxxx</PurchaseMoney > <PurchaseRemark></PurchaseRemark> <ExtendFiled></ExtendFiled> </PurchaseSubOrderItem> </PurchaseSubOrderItemList> </PurchaseSubOrder > <PurchaseSubOrder/> </PurchaseSubOrderList> </DataBody> </DataDocument>
想要快速的获得复杂xml文本中的各个字段值,用微软自带的序列化和反序列化是一种不错的选择。
先根据你的xml结构,定义一个实体类。话不多说,直接上代码:
/// <summary> /// 定义实体类 /// </summary> [XmlRoot("DataDocument")] public class OrderEntity { [XmlElement("DataHead")] public Head head { get; set; } [XmlElement("DataBody")] public Body body { get; set; } } public class Head { [XmlElement("DataVersion")] public string DataVersion { get; set; } [XmlElement("DataType")] public string DataType { get; set; } [XmlElement("DataPageCount")] public int DataPageCount { get; set; } [XmlElement("DataPageNumber")] public int DataPageNumber { get; set; } } public class Body { [XmlArray("PurchaseSubOrderList")] [XmlArrayItem("PurchaseSubOrder")] public List<SubOrder> SubOrder { get; set; } } public class SubOrder { [XmlElement("PurchaseSubOrderHead")] public SubOrderHead SubOrderHead { get; set; } [XmlArray("PurchaseSubOrderItemList")] [XmlArrayItem("PurchaseSubOrderItem")] public List<SubOrderItem> SubOrderItem { get; set; } } public class SubOrderHead { [XmlElement("OrderCode")] public string OrderCode { get; set; } [XmlElement("OrderType")] public int OrderType { get; set; } [XmlElement("OrderLevel")] public int OrderLevel { get; set; } [XmlElement("OrganizationCode")] public string OrganizationCode { get; set; } [XmlElement("OrganizationName")] public string OrganizationName { get; set; } [XmlElement("DepartmentCode")] public string DepartmentCode { get; set; } [XmlElement("DepartmentName")] public string DepartmentName { get; set; } [XmlElement("DepartmentAddress")] public string DepartmentAddress { get; set; } [XmlElement("DeliveryType")] public int DeliveryType { get; set; } [XmlElement("SupplierCode")] public string SupplierCode { get; set; } [XmlElement("SupplierName")] public string SupplierName { get; set; } [XmlElement("OrderRemark")] public string OrderRemark { get; set; } [XmlElement("OrderDate")] public DateTime OrderDate { get; set; } [XmlElement("OrderAmount")] public double OrderAmount { get; set; } [XmlElement("ListCount")] public int ListCount { get; set; } } public class SubOrderItem { [XmlElement("DrugCode")] public string DrugCode { get; set; } [XmlElement("PurchaseCount")] public int PurchaseCount { get; set; } [XmlElement("UnitPrice")] public double UnitPrice { get; set; } [XmlElement("MinUnit")] public int MinUnit { get; set; } [XmlElement("UNIT")] public int UNIT { get; set; } [XmlElement("CargoSpace")] public string CargoSpace { get; set; } [XmlElement("PurchaseMoney")] public double PurchaseMoney { get; set; } [XmlElement("PurchaseRemark")] public string PurchaseRemark { get; set; } [XmlElement("ExtendFiled")] public bool ExtendFiled { get; set; } }
然后,在主程序中实例化这个类,再给对象赋值,再进行序列化。
public static void Main(string[] args) { OrderEntity order = new OrderEntity(); order.head = new Head(); order.head.DataPageCount = 10; order.head.DataPageNumber = 10; order.head.DataType = "type"; order.head.DataVersion = "1.0"; order.body = new Body(); order.body.SubOrder = new List<SubOrder>(); SubOrder so = new SubOrder(); so.SubOrderHead = new SubOrderHead(); so.SubOrderHead.DeliveryType = 1; so.SubOrderHead.DepartmentAddress = "DepartmentAddress"; so.SubOrderHead.DepartmentCode = "DepartmentCode"; so.SubOrderHead.DepartmentName = "DepartmentName"; so.SubOrderHead.ListCount = 100; so.SubOrderHead.OrderAmount = 1000; SubOrderItem item = new SubOrderItem(); item.CargoSpace = "1111"; item.DrugCode = "1234"; item.ExtendFiled = true; item.UNIT = 23; so.SubOrderItem = new List<SubOrderItem>(); so.SubOrderItem.Add(item); order.body.SubOrder.Add(so); XmlSerializer serializer = new XmlSerializer(typeof(OrderEntity)); serializer.Serialize(Console.Out, order); Console.Read(); }
需要注意的是,main函数要加public哦,否则会出现无法序列化的问题。
反序列化就不多说了,网上例子很多。