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


        [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;
                }
            }
  • 相关阅读:
    学习笔记25—python基本运算法则
    学习笔记24—win10环境下python版libsvm的安装
    学习笔记23—window10 64位 python2.7 安装liblinear
    学习笔记22—PS小技巧
    学习笔记21—PS换图片背景
    学习笔记20—MATLAB特殊函数
    学习笔记19—dpabi错误集
    学习笔记18—circos应用集
    system
    Python string
  • 原文地址:https://www.cnblogs.com/wjx-blog/p/8985684.html
Copyright © 2020-2023  润新知