欢迎来到入门教程网!

C#教程

当前位置:主页 > 软件编程 > C#教程 >

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

来源:本站原创|时间:2020-01-10|栏目:C#教程|点击:

本文给大家分享一下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("写入成功");
    }
  }
}

运行结果如图:

生成的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#程序设计有所帮助。

上一篇:C#抓取网页数据 解析标题描述图片等信息 去除HTML标签

栏    目:C#教程

下一篇:C# FileStream文件读写详解

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

本文地址:https://www.xiuzhanwang.com/a1/C_jiaocheng/6563.html

网页制作CMS教程网络编程软件编程脚本语言数据库服务器

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:835971066 | 邮箱:835971066#qq.com(#换成@)

Copyright © 2002-2020 脚本教程网 版权所有