标签:des style blog class code java
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; } } }
博客出处:http://www.cnblogs.com/yukaizhao/archive/2011/07/22/xml-serialization.html
相关:http://jingyan.baidu.com/article/a681b0dec765473b184346d9.html
XmlSerialization 序列化与反序列化,布布扣,bubuko.com
标签:des style blog class code java
原文地址:http://www.cnblogs.com/1017283242zhu/p/3709787.html