码迷,mamicode.com
首页 > 其他好文 > 详细

XmlSerialization 序列化与反序列化

时间:2014-05-07 19:10:55      阅读:387      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   class   code   java   

bubuko.com,布布扣
bubuko.com,布布扣
bubuko.com,布布扣
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace XmlSerialization
{
    class Program
    {
        static void Main(string[] args)
        {
           // DeSerializer();
            //Serializer_Cat();
             //Serializer_Cats();
            CatCollection catCollection = DeSerializer_Cats();
            foreach (Cat cat in catCollection.Cats)
            {
                Console.WriteLine("color:" + cat.Color);
            }
        }
        //序列化对象
        private static void Serializer()
        {
            int i = 10;
            XmlSerializer serializer = new XmlSerializer(typeof(int));
            serializer.Serialize(Console.Out, i);
            Console.Read();
        }
        //反序列化对象
        private static void DeSerializer()
        {
            using (StringReader sr = new StringReader(@"<?xml version=""1.0"" encoding=""gb2312""?><int>10</int>"))
            {
                //声明序列化对象
                XmlSerializer serializer=new XmlSerializer(typeof(int));
                //将反序列化的结果赋值给i
                int i = (int) serializer.Deserialize(sr);
                //输出反序列化结果
                Console.WriteLine("i="+i);
                Console.Read();
            }
        }

        private static void Serializer_Cat()
        {
            var c = new Cat
            {
                Color = "White",
                Speed = 10,
                Saying = "White or black,  so long as the cat can catch mice,  it is a good cat"
            };
            XmlSerializer serializer=new XmlSerializer(typeof(Cat));
            serializer.Serialize(Console.Out,c);
            Console.Read();
        }

        private static void Serializer_Cats()
        {
            var cWhite = new Cat
            {
                Color = "White",
                Speed = 10,
                Saying = "White or black,  so long as the cat can catch mice,  it is a good cat"
            };
            var cBlack = new Cat
            {
                Color = "Black",
                Speed = 20,
                Saying = "White or black,  so long as the cat can catch mice,  it is a good cat"
            };
            CatCollection cc=new CatCollection{Cats = new Cat[]{cWhite,cBlack}};
            try
            {
                XmlSerializer serializer = new XmlSerializer(typeof(CatCollection));
                StreamWriter sw = new StreamWriter(@"1.xml", false);//自动生成一个1.xml文件,设置为true会自动覆盖、追加生成结果
                serializer.Serialize(sw, cc);
                sw.Flush();//清理缓存
                sw.Close();
            }
            catch (Exception)
            {
                
                throw;
            }
        }

        private static CatCollection DeSerializer_Cats()
        {
            try
            {
                XmlSerializer serializer = new XmlSerializer(typeof(CatCollection));
                StreamReader sr = new StreamReader("1.xml");
                CatCollection cc = (CatCollection)serializer.Deserialize(sr);
                Cat[] cats = cc.Cats;
                sr.Close();
                return cc;
            }
            catch (Exception ex)
            {
                
                throw new Exception(ex.Message);
            }
            return default(CatCollection);
        }
    }
   // [Serializable]
    [XmlRoot("Cats")]
    public class CatCollection
    {
        //[XmlElement("cat")] 注释掉后默认的是XmlElement
        //[XmlArray("items"),XmlArrayItem("item")]//自定义XmlElement
        public Cat[] Cats { get; set; }

        public IEnumerator GetEnumerator()
        {
            Cat cat;
            for (int i = 0; i < Cats.Count(); i++)
            {
                cat = Cats[i];
                yield return cat;
            }
        }
    }

   //[XmlRoot("cat")]
    public  class Cat
    {
        [XmlAttribute("color")]
        public  string Color { get; set; }

        [XmlIgnore]
        public  int Speed { get; set; }

        [XmlElement("saying")]
        public  string Saying { get; set; }
    }
}
bubuko.com,布布扣

 

bubuko.com,布布扣

博客出处:http://www.cnblogs.com/yukaizhao/archive/2011/07/22/xml-serialization.html

相关:http://jingyan.baidu.com/article/a681b0dec765473b184346d9.html

bubuko.com,布布扣

XmlSerialization 序列化与反序列化,布布扣,bubuko.com

XmlSerialization 序列化与反序列化

标签:des   style   blog   class   code   java   

原文地址:http://www.cnblogs.com/1017283242zhu/p/3709787.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!