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对象,为系统指定集合中元素的具体类型。

原创粉丝点击