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>;
            }

  • 相关阅读:
    String ,StringBuilder, StringBuffer
    apt-get方式删除软件
    curl命令的使用
    maven自动部署测试Web应用
    几个重要的maven命令
    linux中默认jdk的配置
    登录注册的页面制作
    运用php做投票题,例题
    复选框式查询 例题租房子
    会话用法 和留言板例题
  • 原文地址:https://www.cnblogs.com/zany/p/4459433.html
Copyright © 2011-2022 走看看