实体类
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 public class SaleOrderModel { 2 public SaleOrderModel() { 3 BuyerID = ""; 4 Name = ""; 5 DistributionChannelCode = new SalesAndServiceBusinessAreaModel(); 6 AccountParty = new AccountPartyModel(); 7 Item = new List<ItemModel>(); 8 Ex_ReceiptProvince = ""; 9 } 10 public string BuyerID { get; set; } 11 12 [Description("languageCode=\"ZH\"")] 13 public string Name { get; set; } 14 15 public int DataOriginTypeCode { get; set; } 16 public SalesAndServiceBusinessAreaModel DistributionChannelCode { get; set; } 17 public AccountPartyModel AccountParty { get; set; } 18 [Description("itemScheduleLineListCompleteTransmissionIndicator=\"true\"")] 19 public List<ItemModel> Item { get; set; } 20 [DisplayName("a2t")] 21 public string Ex_ReceiptProvince { get; set; } 22 } 23 24 public class SalesAndServiceBusinessAreaModel { 25 public SalesAndServiceBusinessAreaModel() { 26 DistributionChannelCode = ""; 27 } 28 public string DistributionChannelCode { get; set; } 29 } 30 31 public class AccountPartyModel { 32 public AccountPartyModel() { 33 PartyID = ""; 34 } 35 public string PartyID { get; set; } 36 } 37 38 public class ItemModel { 39 public ItemModel() { 40 ID = ""; 41 Description = ""; 42 ItemProduct = new List<ItemProductModel>(); 43 } 44 public string ID { get; set; } 45 46 [Description("languageCode=\"ZH\"")] 47 public string Description { get; set; } 48 public List<ItemProductModel> ItemProduct { get; set; } 49 } 50 51 public class ItemProductModel { 52 public ItemProductModel() { 53 ProductID = ""; 54 } 55 public string ProductID { get; set; } 56 }
转换方法
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
public string ModelToXml(object model) { string xml = ""; foreach (var item in model.GetType().GetProperties()) { var values = item.GetValue(model, null); var name = item.Name;//字段名 var prefix = (DisplayNameAttribute[])item.GetCustomAttributes(typeof(DisplayNameAttribute), false);//前缀 if (prefix.Length > 0 && !String.IsNullOrEmpty(prefix[0].DisplayName)) { name = prefix[0].DisplayName + ":" + name; } string descriptionName = ""; var suffix = (DescriptionAttribute[])item.GetCustomAttributes(typeof(DescriptionAttribute), false);//描述 if (suffix.Length > 0 && !String.IsNullOrEmpty(suffix[0].Description)) { descriptionName = name + " " + suffix[0].Description; } else { descriptionName = name; } //数组特殊处理 if (item.PropertyType.Name.ToUpper().StartsWith("LIST")) { dynamic list = values; foreach (var l in list) { xml += string.Format("<{0}>", descriptionName); xml += ModelToXml(l); xml += string.Format("</{0}>", name); } } else if (!item.PropertyType.IsPrimitive && !item.PropertyType.Name.ToUpper().StartsWith("STRING")) { xml += string.Format("<{0}>", descriptionName); xml += ModelToXml(values); xml += string.Format("</{0}>", name); } else { xml += string.Format("<{0}>{1}</{2}>", descriptionName, values, name); } } return xml; }
测试例子
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 SaleOrderModel model = new SaleOrderModel(); 2 model.BuyerID = "Example_06"; 3 model.Name = "CallSalesOrderExample1-20180709"; 4 model.DataOriginTypeCode = 1; 5 model.DistributionChannelCode.DistributionChannelCode = "02"; 6 model.AccountParty.PartyID = "100026"; 7 8 ItemModel item = new ItemModel(); 9 item.ID = "10"; 10 item.Description = "强生美瞳艾姗日抛30片装8.5-475"; 11 item.ItemProduct.Add(new ItemProductModel { ProductID = "111111" }); 12 item.ItemProduct.Add(new ItemProductModel { ProductID = "222222" }); 13 14 ItemModel item2 = new ItemModel(); 15 item2.ID = "20"; 16 item2.Description = "强生美瞳艾姗日抛9999"; 17 item2.ItemProduct.Add(new ItemProductModel { ProductID = "44444" }); 18 item2.ItemProduct.Add(new ItemProductModel { ProductID = "555" }); 19 20 model.Item.Add(item); 21 model.Item.Add(item2); 22 model.Ex_ReceiptProvince = "310000"; 23 var xmlStr = ModelToXml(model);
结果:
<BuyerID>Example_06</BuyerID> <Name languageCode="ZH">CallSalesOrderExample1-20180709</Name> <DataOriginTypeCode>1</DataOriginTypeCode> <DistributionChannelCode> <DistributionChannelCode>02</DistributionChannelCode> </DistributionChannelCode> <AccountParty> <PartyID>100026</PartyID> </AccountParty> <Item itemScheduleLineListCompleteTransmissionIndicator="true"> <ID>10</ID> <Description languageCode="ZH">强生美瞳艾姗日抛30片装8.5-475</Description> <ItemProduct> <ProductID>111111</ProductID> </ItemProduct> <ItemProduct> <ProductID>222222</ProductID> </ItemProduct> </Item> <Item itemScheduleLineListCompleteTransmissionIndicator="true"> <ID>20</ID> <Description languageCode="ZH">强生美瞳艾姗日抛9999</Description> <ItemProduct> <ProductID>44444</ProductID> </ItemProduct> <ItemProduct> <ProductID>555</ProductID> </ItemProduct> </Item> <a2t:Ex_ReceiptProvince>310000</a2t:Ex_ReceiptProvince>