本文給大家分享一下C#操作(讀取、寫入)XML文檔的實用方法,這種方法主要是對比較規范的XML文檔進行操作,因為它(XML文檔)和類對象是嚴格對應的,否則在反序列或序列化的時候會出現錯誤。其他的不用多說,直接看示例代碼吧:
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111using
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 =
"[email protected]"
,
RegTime = DateTime.Now
},
new
UserInfo()
{
Id = 2,
UserName =
"李四"
,
UserPass =
"FDASDFAS"
,
Email =
"[email protected]"
,
RegTime = DateTime.Now
},
new
UserInfo()
{
Id = 3,
UserName =
"王五"
,
UserPass =
"FDASDFAS"
,
Email =
"[email protected]"
,
RegTime = DateTime.Now
},
new
UserInfo()
{
Id = 4,
UserName =
"趙六"
,
UserPass =
"FDASDFAS"
,
Email =
"[email protected]"
,
RegTime = DateTime.Now
},
new
UserInfo()
{
Id = 5,
UserName =
"田七"
,
UserPass =
"FDASDFAS"
,
Email =
"[email protected]"
,
RegTime = DateTime.Now
},
new
UserInfo()
{
Id = 6,
UserName =
"王八"
,
UserPass =
"FDASDFAS"
,
Email =
"[email protected]"
,
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
>[email protected]</
Email
>
<
RegTime
>2016-04-21T09:15:58.5576551+08:00</
RegTime
>
</
UserInfo
>
<
UserInfo
>
<
Id
>2</
Id
>
<
UserName
>李四</
UserName
>
<
UserPass
>FDASDFAS</
UserPass
>
<
Email
>[email protected]</
Email
>
<
RegTime
>2016-04-21T09:15:58.5576551+08:00</
RegTime
>
</
UserInfo
>
<
UserInfo
>
<
Id
>3</
Id
>
<
UserName
>王五</
UserName
>
<
UserPass
>FDASDFAS</
UserPass
>
<
Email
>[email protected]</
Email
>
<
RegTime
>2016-04-21T09:15:58.5576551+08:00</
RegTime
>
</
UserInfo
>
<
UserInfo
>
<
Id
>4</
Id
>
<
UserName
>趙六</
UserName
>
<
UserPass
>FDASDFAS</
UserPass
>
<
Email
>[email protected]</
Email
>
<
RegTime
>2016-04-21T09:15:58.5576551+08:00</
RegTime
>
</
UserInfo
>
<
UserInfo
>
<
Id
>5</
Id
>
<
UserName
>田七</
UserName
>
<
UserPass
>FDASDFAS</
UserPass
>
<
Email
>[email protected]</
Email
>
<
RegTime
>2016-04-21T09:15:58.5576551+08:00</
RegTime
>
</
UserInfo
>
<
UserInfo
>
<
Id
>6</
Id
>
<
UserName
>王八</
UserName
>
<
UserPass
>FDASDFAS</
UserPass
>
<
Email
>[email protected]</
Email
>
<
RegTime
>2016-04-21T09:15:58.5576551+08:00</
RegTime
>
</
UserInfo
>
</
ArrayOfUserInfo
>