程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#完成導出List數據到xml文件的辦法【附demo源碼下載】

C#完成導出List數據到xml文件的辦法【附demo源碼下載】

編輯:C#入門知識

C#完成導出List數據到xml文件的辦法【附demo源碼下載】。本站提示廣大學習愛好者:(C#完成導出List數據到xml文件的辦法【附demo源碼下載】)文章只能為提供參考,不一定能成為您想要的結果。以下是C#完成導出List數據到xml文件的辦法【附demo源碼下載】正文


本文實例講述了C#完成導出List數據到xml文件的辦法。分享給年夜家供年夜家參考,詳細以下:

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>

完全實例代碼點擊此處本站下載。

願望本文所述對年夜家C#法式設計有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved