c# 序列化XML文件时,子类的名称命名

2021-04-13 05:26

阅读:494

标签:odi   weight   文件   sch   color   except   xml文件   class   nbsp   

    [XmlRoot(ElementName = "product")]
    public class WMS_Query_ProductInfo
    {
        public string skuCode { get; set; }
        public float normalQuantity { get; set; }
        public float defectiveQuantity { get; set; }
        public float averageWeight { get; set; }
        public int? lineNo { get; set; }
        [XmlArray("batchs"), XmlArrayItem("batch")]
        public List batchs { get; set; }
    }
 
    public class WMS_Query_Batch
    {
        public string fixStatusCode { get; set; }
        public DateTime? productionDate { get; set; }
        public DateTime? expiryDate { get; set; }
        public string packCode { get; set; }
        public string uomCode { get; set; }
        public decimal? quantity { get; set; }
    }
上面两个类序列化后的文件如下:
xml version="1.0" encoding="utf-8" standalone="yes"?>
product>
    skuCode>dddskuCode>
    normalQuantity>0normalQuantity>
    defectiveQuantity>0defectiveQuantity>
    averageWeight>0averageWeight>
    lineNo p2:nil="true"
        xmlns:p2="http://www.w3.org/2001/XMLSchema-instance" />
        batchs>
            batch>
                productionDate p4:nil="true"
                    xmlns:p4="http://www.w3.org/2001/XMLSchema-instance" />
                    expiryDate>2018-05-03T15:38:03.2177502+08:00expiryDate>
                    packCode>ddfpackCode>
                    uomCode>dduomCode>
                    quantity>1212quantity>
                batch>
            batchs>
        product>

关键点:

        [XmlArray("batchs"), XmlArrayItem("batch")]
        public ListWMS_Query_Batch> batchs { get; set; }

 

 

序列化的代码如下:

            WMS_Query_ProductInfo product = new WMS_Query_ProductInfo();
            product.skuCode = "ddd";
            product.batchs = new List();
            product.batchs.Add(new WMS_Query_Batch { expiryDate = DateTime.Now, packCode = "ddf", quantity = 1212, uomCode = "dd" });
            string str = XmlUtil.Serializer(product);
        public static object Deserialize(Type type, string xml)
        {
            try
            {
                using (StringReader sr = new StringReader(xml))
                {
                    XmlSerializer xmldes = new XmlSerializer(type);
                    return xmldes.Deserialize(sr);
                }
            }
            catch (Exception e)
            {
                Log.Log4net.Error(e.Message);
                return null;
            }
        }

 

c# 序列化XML文件时,子类的名称命名

标签:odi   weight   文件   sch   color   except   xml文件   class   nbsp   

原文地址:https://www.cnblogs.com/wjx-blog/p/8985684.html

上一篇:C#关键字—方法参数

下一篇:C#常量


评论


亲,登录后才可以留言!