zoukankan      html  css  js  c++  java
  • C# 序列化和反序列化(xml 文件)

    序列化是将对象保存为文本文件或二进制文件;

    反序列化则是读取文件信息,还原为对象;

    序列化保存为文本内容,主要是 xml 和 json 两种,这里介绍序列化为 xml 文件的方式。

    想要序列化,先要在类上添加 [Serializable] 特性标签,如:

        [Serializable]
        public class Person {
            private string test1 = "test1";
            protected string test2 = "test2";
            public string test3 = "test3";
            internal string test4 = "test4";
            public int id { get; set; }
            public string name { get; set; }
            public int age { get; set; }
    
            public override string ToString() {
                return $"[id={id}, name={name}, age={age}, test1={test1}, test2={test2}, test4={test3}, test4={test4}]";
            }
        }

    C# 中处理 xml 序列化的相关类都在 System.Xml.Serialization 命名空间下,这里通过使用 XmlSerializer 类来实现序列化和反序列化:

    public class xml_serializer_manager{
        /// <summary>
        /// serialize object to xml file.
        /// </summary>
        /// <param name="path">the path to save the xml file</param>
        /// <param name="obj">the object you want to serialize</param>
        public void serialize_to_xml(string path, object obj) {
            XmlSerializer serializer = new XmlSerializer(obj.GetType());
            string content = string.Empty;
            //serialize
            using (StringWriter writer = new StringWriter()) {
                serializer.Serialize(writer, obj);
                content = writer.ToString();
            }
            //save to file
            using (StreamWriter stream_writer = new StreamWriter(path)) {
                stream_writer.Write(content);
            }
        }
    
        /// <summary>
        /// deserialize xml file to object
        /// </summary>
        /// <param name="path">the path of the xml file</param>
        /// <param name="object_type">the object type you want to deserialize</param>
        public object deserialize_from_xml(string path, Type object_type) {
            XmlSerializer serializer = new XmlSerializer(object_type);
            using (StreamReader reader = new StreamReader(path)) {
                return serializer.Deserialize(reader);
            }
        }
    }

    使用方法:

    序列化:

    xml_serializer_manager manager = new xml_serializer_manager();
    string path = @"D:person.xml";
    Person p = new Person { id=1001, name="tommy", age=30};
    manager.serialize_to_xml(path, p);

    结果如下:

    生成 person.xml 文件,而且只处理了 public 修饰的属性,其他都不处理

    反序列化:

    xml_serializer_manager manager = new xml_serializer_manager();
    string path = @"D:person.xml";
    Person p = (Person)manager.deserialize_from_xml(path, typeof(Person));
    Console.Write(p.ToString());

    结果:

  • 相关阅读:
    appium启动android和iOS真机
    文本编辑器Sublime Text 3 -mac版简体中文汉化教程
    Navicat Premium Mac 安装
    mac下安装jmeter
    Pycharm 配置git
    mac下jdk安装配置
    WebDriverAgentRunner安装成功后build的常见报错
    WebDriverAgent入门篇-安装和使用
    macOS升级
    Bash on windows从14.0升级到ubuntu16.04
  • 原文地址:https://www.cnblogs.com/tommy-huang/p/11283529.html
Copyright © 2011-2022 走看看