欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#操作读取、写入XML文档的实用方法

程序员文章站 2023-10-31 16:04:16
本文给大家分享一下c#操作(读取、写入)xml文档的实用方法,这种方法主要是对比较规范的xml文档进行操作,因为它(xml文档)和类对象是严格对应的,否则在反序列或序列化的...

本文给大家分享一下c#操作(读取、写入)xml文档的实用方法,这种方法主要是对比较规范的xml文档进行操作,因为它(xml文档)和类对象是严格对应的,否则在反序列或序列化的时候会出现错误。其他的不用多说,直接看示例代码吧:

using system;
using system.collections.generic;
using system.io;
using system.linq;
using system.text;
using system.threading.tasks;
using system.xml.serialization;

namespace _01xmlserialize
{
  class program
  {
    private static list<userinfo> list = new list<userinfo>()
    {
      new userinfo()
      {
        id = 1,
        username = "张三",
        userpass = "fdasdfas",
        email = "zhangsan@163.com",
        regtime = datetime.now
      },
      new userinfo()
      {
        id = 2,
        username = "李四",
        userpass = "fdasdfas",
        email = "lisi@163.com",
        regtime = datetime.now
      },
      new userinfo()
      {
        id = 3,
        username = "王五",
        userpass = "fdasdfas",
        email = "wangwu@163.com",
        regtime = datetime.now
      },
      new userinfo()
      {
        id = 4,
        username = "赵六",
        userpass = "fdasdfas",
        email = "zhaoliu@163.com",
        regtime = datetime.now
      },
      new userinfo()
      {
        id = 5,
        username = "田七",
        userpass = "fdasdfas",
        email = "tianqi@163.com",
        regtime = datetime.now
      },
      new userinfo()
      {
        id = 6,
        username = "王八",
        userpass = "fdasdfas",
        email = "wangba@163.com",
        regtime = datetime.now
      }
    };
    static void main(string[] args)
    {
      //序列化对象生成文件
      writesinglexml();

      //反序列化文件生成对象
      readlistxml();

      console.readkey();
    }

    /// <summary>
    /// 从xml文件中反序列化对象
    /// </summary>
    private static void readlistxml()
    {
      //反序列化文件
      console.writeline("反序列化xml文件为集合");
      var reader = new xmlserializer(typeof(list<userinfo>));

      using (var fs = file.openread("userinfo.xml"))
      {
        var result = reader.deserialize(fs) as list<userinfo>;

        result.foreach(console.writeline);
      }
      console.writeline("反序列化成功");
    }

    /// <summary>
    /// 序列化对象到xml文件
    /// </summary>
    private static void writesinglexml()
    {
      //序列化集合为xml文件
      console.writeline("序列化集合为xml文件");

      var reader = new xmlserializer(typeof(list<userinfo>));
      //写入文件
      using (var fs = file.openwrite("userinfo.xml"))
      {
        reader.serialize(fs, list);
      }

      console.writeline("写入成功");
    }
  }
}

运行结果如图:

C#操作读取、写入XML文档的实用方法

生成的xml文档如下:

<?xml version="1.0"?>
<arrayofuserinfo xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:xsd="http://www.w3.org/2001/xmlschema">
 <userinfo>
  <id>1</id>
  <username>张三</username>
  <userpass>fdasdfas</userpass>
  <email>zhangsan@163.com</email>
  <regtime>2016-04-21t09:15:58.5576551+08:00</regtime>
 </userinfo>
 <userinfo>
  <id>2</id>
  <username>李四</username>
  <userpass>fdasdfas</userpass>
  <email>lisi@163.com</email>
  <regtime>2016-04-21t09:15:58.5576551+08:00</regtime>
 </userinfo>
 <userinfo>
  <id>3</id>
  <username>王五</username>
  <userpass>fdasdfas</userpass>
  <email>wangwu@163.com</email>
  <regtime>2016-04-21t09:15:58.5576551+08:00</regtime>
 </userinfo>
 <userinfo>
  <id>4</id>
  <username>赵六</username>
  <userpass>fdasdfas</userpass>
  <email>zhaoliu@163.com</email>
  <regtime>2016-04-21t09:15:58.5576551+08:00</regtime>
 </userinfo>
 <userinfo>
  <id>5</id>
  <username>田七</username>
  <userpass>fdasdfas</userpass>
  <email>tianqi@163.com</email>
  <regtime>2016-04-21t09:15:58.5576551+08:00</regtime>
 </userinfo>
 <userinfo>
  <id>6</id>
  <username>王八</username>
  <userpass>fdasdfas</userpass>
  <email>wangba@163.com</email>
  <regtime>2016-04-21t09:15:58.5576551+08:00</regtime>
 </userinfo>
</arrayofuserinfo>

以上就是本文的全部内容,希望对大家学习c#程序设计有所帮助。