实体类转为自定义xml

实体类

 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     }
View Code

转换方法

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;
        }
View Code

测试例子

 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);
View Code

结果:

<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>

 

转载于:https://www.cnblogs.com/ywfy/p/9390792.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值