C#匯出List資料到xml檔案的源碼分享

來源:互聯網
上載者:User
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>
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.