zoukankan      html  css  js  c++  java
  • C#学习记录(八) XML Serializer尝试

    将数据转为xml并写入文件

    static void Main(string[] args)
            {
                List<Customer> customers = new List<Customer>(){
                        new Customer() { FirstName = "Vars", LastName = "Hoo", Email = "vv@yahoo.com"},
                        new Customer() { FirstName = "Tes", LastName = "Qerr", Email = "tes123@qq.com"},
                        new Customer() { FirstName = "Hum", LastName = "Ltx", Email = "hl@163.com"}
                };
    
                XmlSerializer serializer = new XmlSerializer(typeof(List<Customer>));
    
                StreamWriter file = new StreamWriter(@"f:	est.xml");
                serializer.Serialize(file, customers);
            }

    运行后相应位置的文件

    如果更改Customer类中的内容

    public class Customer
        {
            [XmlAttribute()]
            public string FirstName { get; set; }
            [XmlIgnore()]
            public string LastName { get; set; }
            public string Email { get; set; }
    
            public override string ToString()
            {
                return string.Format("{0} {1}
    Email: {2}", FirstName, LastName, Email);
            }
        }

    输出的文件中的内容变为

    接下来利用先前生成的test.xml文件,将其内容读入

    static void Main(string[] args)
            {
                XmlSerializer serializer = new XmlSerializer(typeof(List<Customer>));
    
                StreamReader file = new StreamReader(@"f:	est.xml");
    
                List<Customer> customers = serializer.Deserialize(file) as List<Customer>;
            }

  • 相关阅读:
    CentOS7 安装 Mysql 服务
    git 第一次 push 遇到问题
    为什么PHP(CLI)同一个错误信息会打印两次?
    python密码输入模块getpass
    Linux安装JDK详细步骤
    嘿嘿嘿,开始自学mysql
    Bable实现由ES6转译为ES5
    AJAX
    模板层
    lshw查看系统硬件信息
  • 原文地址:https://www.cnblogs.com/zany/p/4459433.html
Copyright © 2011-2022 走看看