exercise: 序列化和反序列化Xml --CSharp

 

    class XmlHelper
    {


        public XmlHelper()
        {
        }



        /// <summary>
        /// 读取 xml 文件内容
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static T Load<T>(string fileName)
        {
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));

            using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
            {
                return (T)xmlSerializer.Deserialize(fs);
            }
        }

        /// <summary>
        /// 写入 xml 文件
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="fileName"></param>
        /// <param name="data"></param>
        public static void Write<T>(string fileName, T data)
        {
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));

            using (var fs = new FileStream(fileName, FileMode.Truncate, FileAccess.Write))
            {
                xmlSerializer.Serialize(fs, data);
            }
        }

    }

 

    class SomeXmlConfig
    {

        [XmlElement("hello")]
        public string Hello { get; set; }

        [XmlElement("world")]
        public string World { get; set; }


        public SomeXmlConfig()
        {
        }

    }

 

 

 

 

 

--- THE END ---

 

上一篇:ArcObject对象序列化和反序列化


下一篇:实践——ELF文件格式分析