C#匯出List資料到xml檔案,這裡主要用到的是:
XmlSerializer 類 (System.Xml.Serialization)
將對象序列化到 XML 文檔中和從 XML 文檔中還原序列化對象。XmlSerializer 使您得以控制如何將對象編碼到 XML 中。
實體類代碼:
/// <summary> /// 使用者實體類 /// /// 注意:類的存取修飾詞必須是:public,否則會出現錯誤: /// 因其保護層級而不可訪問。只能處理公用類型。 /// </summary> [Serializable] public class UserModel { private long id = 1; private string name = "name"; private int gender = 0; private int age = 20; private string userName = "userName"; private string password = "password"; public long Id { get { return id; } set { id = value; } } public string Name { get { return name; } set { name = value; } } public int Gender { get { return gender; } set { gender = value; } } public int Age { get { return age; } set { age = value; } } public string UserName { get { return userName; } set { userName = value; } } public string Password { get { return password; } set { password = value; } } }
匯出代碼:
private void button1_Click(object sender, EventArgs e) { try { List<UserModel> list = new List<UserModel>(); list.Add(new UserModel()); list.Add(new UserModel()); list.Add(new UserModel()); list.Add(new UserModel()); list.Add(new UserModel()); list.Add(new UserModel()); list.Add(new UserModel()); list.Add(new UserModel()); list.Add(new UserModel()); list.Add(new UserModel()); using (System.IO.StringWriter stringWriter = new StringWriter(new StringBuilder())) { XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<UserModel>)); xmlSerializer.Serialize(stringWriter, list); FileStream fs = new FileStream("list_userModel.xml", FileMode.OpenOrCreate); StreamWriter sw = new StreamWriter(fs); sw.Write(stringWriter.ToString()); sw.Close(); fs.Close(); MessageBox.Show("寫入檔案成功!"); } } catch (System.Exception ex) { System.Console.WriteLine("===================================="); System.Console.WriteLine(ex.Message); System.Console.WriteLine(ex.StackTrace); } }
產生的XML檔案內容:
<?xml version="1.0" encoding="utf-16"?><ArrayOfUserModel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <UserModel> <Id>1</Id> <Name>name</Name> <Gender>0</Gender> <Age>20</Age> <UserName>userName</UserName> <Password>password</Password> </UserModel> <UserModel> <Id>1</Id> <Name>name</Name> <Gender>0</Gender> <Age>20</Age> <UserName>userName</UserName> <Password>password</Password> </UserModel> <UserModel> <Id>1</Id> <Name>name</Name> <Gender>0</Gender> <Age>20</Age> <UserName>userName</UserName> <Password>password</Password> </UserModel> <UserModel> <Id>1</Id> <Name>name</Name> <Gender>0</Gender> <Age>20</Age> <UserName>userName</UserName> <Password>password</Password> </UserModel> <UserModel> <Id>1</Id> <Name>name</Name> <Gender>0</Gender> <Age>20</Age> <UserName>userName</UserName> <Password>password</Password> </UserModel> <UserModel> <Id>1</Id> <Name>name</Name> <Gender>0</Gender> <Age>20</Age> <UserName>userName</UserName> <Password>password</Password> </UserModel> <UserModel> <Id>1</Id> <Name>name</Name> <Gender>0</Gender> <Age>20</Age> <UserName>userName</UserName> <Password>password</Password> </UserModel> <UserModel> <Id>1</Id> <Name>name</Name> <Gender>0</Gender> <Age>20</Age> <UserName>userName</UserName> <Password>password</Password> </UserModel> <UserModel> <Id>1</Id> <Name>name</Name> <Gender>0</Gender> <Age>20</Age> <UserName>userName</UserName> <Password>password</Password> </UserModel> <UserModel> <Id>1</Id> <Name>name</Name> <Gender>0</Gender> <Age>20</Age> <UserName>userName</UserName> <Password>password</Password> </UserModel></ArrayOfUserModel>