zoukankan      html  css  js  c++  java
  • xml序列化

    public static void SaveToXml(string filePath, object sourceObj, Type type, string xmlRootName)
            {
                if (!string.IsNullOrWhiteSpace(filePath) && sourceObj != null)
                {
                    type = type != null ? type : sourceObj.GetType();
    
                    using (StreamWriter writer = new StreamWriter(filePath))
                    {
                        System.Xml.Serialization.XmlSerializer xmlSerializer = string.IsNullOrWhiteSpace(xmlRootName) ?
                            new System.Xml.Serialization.XmlSerializer(type) :
                            new System.Xml.Serialization.XmlSerializer(type, new XmlRootAttribute(xmlRootName));
                        xmlSerializer.Serialize(writer, sourceObj);
                    }
                }
            }
    
            public static object LoadFromXml(string filePath, Type type)
            {
                object result = null;
    
                if (File.Exists(filePath))
                {
                    using (StreamReader reader = new StreamReader(filePath))
                    {
                        System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(type);
                        result = xmlSerializer.Deserialize(reader);
                    }
                }
    
                return result;
            }



    //主类特性
       [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
        [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
       
    //属性特性
            /// <remarks/>
            [System.Xml.Serialization.XmlElementAttribute("AirLineCompany")]
    //序列化指定特定的节点名称 
    [System.Xml.Serialization.XmlType("AirLine")] public class Ret_AirLine { public string DepartureAirport { get; set; } public string ArrivalAirport { get; set; } /// <summary> /// PassengerName PassengerType IdNo KeepFee ReturnMoney /// </summary> public List<Passenger> PassengerList { get; set; } = new List<Passenger>(); }

      

      

      

  • 相关阅读:
    P4049 [JSOI2007]合金
    CF1073C Vasya and Robot
    输出100以内奇数,偶数,质数,合数的脚本
    取/etc/password文件最后一个单词的最后一个字符
    window下进程退出后自动重启
    如何让DOS命令在新窗口打开
    dos命令关闭所有dos窗口
    使用jps查看JVM进程信息
    windows .bat批处理实现进程监控确保程序运行
    经典博客4(六尺帐篷)
  • 原文地址:https://www.cnblogs.com/guosier/p/5681069.html
Copyright © 2011-2022 走看看