如何在C#里序列化集合对象

Collection主要是指像Array, ArrayList, List, Dictionary, HashTable这些数据类型,大家平时用的很多。如果一个类中有一个Collection类型的成员,在对这个类进行XML序列化的时候,应该如何处理?应该说在.net当中这是比较简单的,只要建立一个XmlSerializer类就可以帮你自动搞定,不过有的时候你可能需要对自动的序列化过程施加更多的控制,比如XML的结构是实现固定的,你必须按照要求去生成XML结构。

使用不同的属性可以灵活的控制生成的XML,这里我就不多介绍了,主要讲一下如何序列化比较复杂的Collection结构。下面的方法,对于所有实现了IEnumerable接口的Collection都有效。

我使用MSDN中的例子,不过没有使用数组或者ArrayList,而是使用了比较高级的数据类型List<T>,希望在讲解如何序列化XML的同时给使用List<T>的同学提供点参考。

序列化一个List<T>

下面的代码示范了如何序列化一个 List<T>,实际上和序列化其它类一样,把这个类扔给Serialize()函数即可。
  1. using System;

  2. using System.Collections.Generic;

  3. using System.Linq;

  4. using System.Text;

  5. using System.Xml.Serialization;

  6. using System.IO;



  7. namespace SerializeCollection

  8. {

  9.     class Program

  10.     {

  11.         static void Main(string[] args)

  12.         {

  13.             Program test = new Program();

  14.             test.SerializeDocument("e:\\books.xml");

  15.         }



  16.         public void SerializeDocument(string filename)

  17.         {

  18.             // Creates a new XmlSerializer.

  19.             XmlSerializer s =

  20.             new XmlSerializer(typeof(MyRootClass));



  21.             // Writing the file requires a StreamWriter.

  22.             TextWriter myWriter = new StreamWriter(filename);



  23.             // Creates an instance of the class to serialize. 

  24.             MyRootClass myRootClass = new MyRootClass();

  25.             

  26.             //create items

  27.             Item item1 = new Item();

  28.             // Sets the objects' properties.

  29.             item1.ItemName = "Widget1";

  30.             item1.ItemCode = "w1";

  31.             item1.ItemPrice = 231;

  32.             item1.ItemQuantity = 3;



  33.             

  34.             Item item2 = new Item();

  35.             // Sets the objects' properties.

  36.             item2.ItemName = "Widget2";

  37.             item2.ItemCode = "w2";

  38.             item2.ItemPrice = 800;

  39.             item2.ItemQuantity = 2;



  40.             // Sets the class's Items property to the list.

  41.             myRootClass.Items.Add(item1);

  42.             myRootClass.Items.Add(item2);



  43.             /* Serializes the class, writes it to disk, and closes 

  44.               the TextWriter. */

  45.             s.Serialize(myWriter, myRootClass);

  46.             myWriter.Close();

  47.         }



  48.     }



  49.     // This is the class that will be serialized.

  50.     [Serializable]

  51.     public class MyRootClass

  52.     {

  53.         public MyRootClass()

  54.         {

  55.             items = new List<Item>();

  56.         }



  57.         private List<Item> items;



  58.         public List<Item> Items

  59.         {

  60.             get { return items; }

  61.             set { items = value; }

  62.         }

  63.     }



  64.     public class Item

  65.     {

  66.         [XmlElement(ElementName = "OrderItem")]

  67.         public string ItemName;

  68.         public string ItemCode;

  69.         public decimal ItemPrice;

  70.         public int ItemQuantity;

  71.     }





  72. }
复制代码
最后序列化成的XML:
  1. <?xml version="1.0" encoding="utf-8"?>

  2. <MyRootClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

  3.   <Items>

  4.     <Item>

  5.       <OrderItem>Widget1</OrderItem>

  6.       <ItemCode>w1</ItemCode>

  7.       <ItemPrice>231</ItemPrice>

  8.       <ItemQuantity>3</ItemQuantity>

  9.     </Item>

  10.     <Item>

  11.       <OrderItem>Widget2</OrderItem>

  12.       <ItemCode>w2</ItemCode>

  13.       <ItemPrice>800</ItemPrice>

  14.       <ItemQuantity>2</ItemQuantity>

  15.     </Item>

  16.   </Items>

  17. </MyRootClass>
复制代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值