C# 串行化与反串行化--使用XmlSerializer进行串行化(另外一种方法)
来源:互联网 发布:seo超级伪静态 v3.1.0 编辑:程序博客网 时间:2024/05/16 06:17
4、使用XmlSerializer进行串行化(另外一种方法)
XmlSerializer串行化除了使用[Serializable()]特性外,还有一种方式:使用XmlRoot、XmlAttribute、XmlElement等特性直接标记数据成员。
[XmlRoot()] public class Product { private int prodId; private string prodName; private int catId; private int disc; [XmlAttribute("Discount")] public int Discount { get { return disc; } set { disc = value; } } [XmlElement()] public int ProductID { get { return prodId; } set { prodId = value; } } [XmlElement()] public string ProductName { get { return prodName; } set { prodName = value; } } [XmlElement()] public int CatID { get { return catId; } set { catId = value; } } } public class BookProduct : Product { private string isbnNum; public string ISBnNum { get { return isbnNum; } set { isbnNum = value; } } } public class Inverntory { private Product[] stuff; public Product[] InventoryItems { get { return stuff; } set { stuff = value; } } } public sealed class ConfigurationManagerInverntory { private static string path = System.Windows.Forms.Application.StartupPath + "\\Inverntory.xml"; public static Inverntory Get() { if (!File.Exists(path)) return null; XmlAttributes attrs = new XmlAttributes(); attrs.XmlElements.Add(new XmlElementAttribute("Book", typeof(BookProduct))); attrs.XmlElements.Add(new XmlElementAttribute("Product", typeof(Product))); XmlAttributeOverrides attrOver = new XmlAttributeOverrides(); attrOver.Add(typeof(Inverntory), "InventoryItems", attrs); XmlSerializer xs = new XmlSerializer(typeof(Inverntory), attrOver); using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read)) { return (Inverntory)xs.Deserialize(fs); } } public static void Set(Inverntory hr) { XmlAttributes attrs = new XmlAttributes(); attrs.XmlElements.Add(new XmlElementAttribute("Book", typeof(BookProduct))); attrs.XmlElements.Add(new XmlElementAttribute("Product", typeof(Product))); XmlAttributeOverrides attrOver = new XmlAttributeOverrides(); attrOver.Add(typeof(Inverntory), "InventoryItems", attrs); XmlSerializer xs = new XmlSerializer(typeof(Inverntory), attrOver); using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write)) { xs.Serialize(fs, hr); } } }
备注:
上面列出了一个比较复杂的串行化的示例。在使用这种方式串行化对象时,需要使用XmlAttributeOverrides来定义XmlSerializer对象,XmlAttributeOverrides对象中包含了集合中元素的具体类型。
在序列化和反序列化时,都需要定义XmlAttributeOverrides对象,为系统指定集合中元素的具体类型。
- C# 串行化与反串行化--使用XmlSerializer进行串行化(另外一种方法)
- C# 串行化与反串行化--使用XmlSerializer进行串行化(另外一种方法)
- C# 串行化与反串行化--使用XmlSerializer进行串行化
- C# 串行化与反串行化--使用XmlSerializer进行串行化
- C# 串行化与反串行化--使用BinaryFormatter进行串行化
- C# 串行化与反串行化--使用SoapFormatter进行串行化
- C# 串行化与反串行化--使用BinaryFormatter进行串行化
- C# 串行化与反串行化--使用SoapFormatter进行串行化
- C# 串行化与反串行化
- C# 串行化与反串行化
- C# 串行化与反串行化--自定义序列化
- C# 串行化与反串行化--自定义序列化
- C#基础知识回顾--串行化与反串行化
- 深入浅出之C# 串行化与反串行化
- 对象串行化---XmlSerializer简介
- 对象串行化和反串行化
- PHP 之 串行化与反串行化
- PHP对象的串行化与反串行化
- C++网络通信框架 ACE
- STM32学习笔记(4):通用定时器基本定时功能
- 为雅安祈福
- Android中的Handler, Looper, MessageQueue和Thread
- MongoDB设置访问权限、设置用户
- C# 串行化与反串行化--使用XmlSerializer进行串行化(另外一种方法)
- memcpy 跟strcpy的区别
- STM32通用定时器(TIM2-5)基本用法
- ie6、ie7下overflow失效
- C# 串行化与反串行化--自定义序列化
- 9个offer,12家公司,35场面试,从微软到谷歌
- PPT2010中轻松实现PPT自动循环播放
- 第五篇 RMAN基础知识补充 一 --转自君三思
- 排序算法总结