C#操作讀取、寫入XML文檔的適用辦法。本站提示廣大學習愛好者:(C#操作讀取、寫入XML文檔的適用辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#操作讀取、寫入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 = "[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>
以上就是本文的全體內容,願望對年夜家進修C#法式設計有所贊助。