码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2018-05-03 16:31:23      阅读:240      评论:0      收藏:0      [点我收藏+]

标签: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<WMS_Query_Batch> 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>ddd</skuCode>
    <normalQuantity>0</normalQuantity>
    <defectiveQuantity>0</defectiveQuantity>
    <averageWeight>0</averageWeight>
    <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:00</expiryDate>
                    <packCode>ddf</packCode>
                    <uomCode>dd</uomCode>
                    <quantity>1212</quantity>
                </batch>
            </batchs>
        </product>


关键点:
        [XmlArray("batchs"), XmlArrayItem("batch")]
        public List<WMS_Query_Batch> batchs { get; set; }

 

 

序列化的代码如下:

            WMS_Query_ProductInfo product = new WMS_Query_ProductInfo();
            product.skuCode = "ddd";
            product.batchs = new List<WMS_Query_Batch>();
            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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!