C# 串行化与反串行化--使用XmlSerializer进行串行化(另外一种方法)

4、使用XmlSerializer进行串行化(另外一种方法)
XmlSerializer串行化除了使用[Serializable()]特性外,还有一种方式:使用XmlRoot、XmlAttribute、XmlElement等特性直接标记数据成员。

[csharp]  view plain  copy
  1. [XmlRoot()]  
  2.     public class Product  
  3.     {  
  4.         private int prodId;  
  5.         private string prodName;  
  6.         private int catId;  
  7.         private int disc;  
  8.   
  9.         [XmlAttribute("Discount")]  
  10.         public int Discount  
  11.         {  
  12.             get { return disc; }  
  13.             set { disc = value; }  
  14.         }  
  15.   
  16.         [XmlElement()]  
  17.         public int ProductID  
  18.         {  
  19.             get { return prodId; }  
  20.             set { prodId = value; }  
  21.         }  
  22.   
  23.         [XmlElement()]  
  24.         public string ProductName  
  25.         {  
  26.             get { return prodName; }  
  27.             set { prodName = value; }  
  28.         }  
  29.   
  30.         [XmlElement()]  
  31.         public int CatID  
  32.         {  
  33.             get { return catId; }  
  34.             set { catId = value; }  
  35.         }  
  36.     }  
  37.   
  38.     public class BookProduct : Product  
  39.     {  
  40.         private string isbnNum;  
  41.         public string ISBnNum  
  42.         {  
  43.             get { return isbnNum; }  
  44.             set { isbnNum = value; }  
  45.         }  
  46.     }  
  47.   
  48.     public class Inverntory  
  49.     {  
  50.         private Product[] stuff;  
  51.   
  52.         public Product[] InventoryItems  
  53.         {  
  54.             get { return stuff; }  
  55.             set { stuff = value; }  
  56.         }  
  57.     }  
  58.   
  59.     public sealed class ConfigurationManagerInverntory  
  60.     {  
  61.         private static string path = System.Windows.Forms.Application.StartupPath + "\\Inverntory.xml";  
  62.   
  63.         public static Inverntory Get()  
  64.         {  
  65.             if (!File.Exists(path))  
  66.                 return null;  
  67.             XmlAttributes attrs = new XmlAttributes();  
  68.             attrs.XmlElements.Add(new XmlElementAttribute("Book"typeof(BookProduct)));  
  69.             attrs.XmlElements.Add(new XmlElementAttribute("Product"typeof(Product)));  
  70.             XmlAttributeOverrides attrOver = new XmlAttributeOverrides();  
  71.             attrOver.Add(typeof(Inverntory), "InventoryItems", attrs);  
  72.   
  73.             XmlSerializer xs = new XmlSerializer(typeof(Inverntory), attrOver);  
  74.             using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))  
  75.             {  
  76.                 return (Inverntory)xs.Deserialize(fs);  
  77.             }  
  78.         }  
  79.   
  80.         public static void Set(Inverntory hr)  
  81.         {  
  82.             XmlAttributes attrs = new XmlAttributes();  
  83.             attrs.XmlElements.Add(new XmlElementAttribute("Book"typeof(BookProduct)));  
  84.             attrs.XmlElements.Add(new XmlElementAttribute("Product"typeof(Product)));  
  85.             XmlAttributeOverrides attrOver = new XmlAttributeOverrides();  
  86.             attrOver.Add(typeof(Inverntory), "InventoryItems", attrs);  
  87.   
  88.             XmlSerializer xs = new XmlSerializer(typeof(Inverntory), attrOver);  
  89.             using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write))  
  90.             {  
  91.                 xs.Serialize(fs, hr);  
  92.             }  
  93.         }  
  94.     }  

备注:

上面列出了一个比较复杂的串行化的示例。在使用这种方式串行化对象时,需要使用XmlAttributeOverrides来定义XmlSerializer对象,XmlAttributeOverrides对象中包含了集合中元素的具体类型。

在序列化和反序列化时,都需要定义XmlAttributeOverrides对象,为系统指定集合中元素的具体类型。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值