参考了Fish LI的Xml读取文章,写了XML序列化和反序列化的文章。
序列化:把实体列转化成XML。反序列化:把XML按一定的规则转化成需要的实体列。
序列化和反序列化化使用到的类,
using System.Xml.Serialization;
using System.IO;
using System.Xml;
XML一些属性:
XMLAttribtue:属性
XMLText:xml值
XmlElement:xml标签值
XML调试,反序列化失败。常见的原因是XML的定义和Class类不一致,导致失败。我的做法是把class序列化,比较序列化的值和需要反序列化的XML有什么不同。
代码使用的类,Class C
public class C { public string Company { get; set; } [XmlElement("B2")] public List<B> b { get; set; } } public class B { [XmlAttribute("sex")] public string B_Attribute { get; set; } [XmlAttribute("age")] public string B_Attribute_Age { get; set; } [XmlText] public string B_Name { get; set; } }
1. 序列化代码使用到的方法类,MemoryStream 初始化流,XmlWriter 创建Stream流可读写,serializer.Serialize序列化操作。StreamReader 读取序列化的流信息。
代码,
public static string XmlSerialize(object o, Encoding encoding) { XmlSerializer serializer = new XmlSerializer(o.GetType()); // StreamWriter 方法 //using (MemoryStream stream = new MemoryStream()) //{ // using (StreamWriter writer = new StreamWriter(stream, encoding)) // { // serializer.Serialize(writer, o); // stream.Position = 0; // using (StreamReader reader = new StreamReader(stream, encoding)) // { // return reader.ReadToEnd(); // } // } //} // XmlWriting方法 using (MemoryStream stream = new MemoryStream()) { using (XmlWriter writer = XmlWriter.Create(stream)) { serializer.Serialize(writer, o); } stream.Position = 0; using (StreamReader reader = new StreamReader(stream, encoding)) { return reader.ReadToEnd(); } } }
2.反序列化化使用到的方法类,MemoryStream 流的形式初始化XML到内存中,StreamReader读取XML二进制信息,mySerializer.Deserialize反序列化为Class C
public static T XmlDeserialize<T>(string s, Encoding encoding) { XmlSerializer mySerializer = new XmlSerializer(typeof(T)); using (MemoryStream ms = new MemoryStream(encoding.GetBytes(s))) { using (StreamReader sr = new StreamReader(ms, encoding)) { return (T)mySerializer.Deserialize(sr); } } }
所有的代码如下,
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml.Serialization; using System.IO; using System.Xml; namespace WebService { class Program { static void Main(string[] args) { //C c = new C(); //List<B> listB = new List<B>(); //B b = new B(); //b.A = "男"; //listB.Add(b); //b = new B(); //b.A = "女"; //listB.Add(b); //b = new B(); //b.A = "男"; //listB.Add(b); //c.b = listB; //string str = XmlSerialize(c, Encoding.UTF8); //Console.WriteLine(str); //C cClass = XmlDeserialize<C>(@"<C><B2><A sex=""男"" /><AA>陈建</AA></B2></C>", Encoding.UTF8); // 最主要的是类和xml信息能对应,反序列化失败的查找方法,先序列化出来一个XML,对比XML值。 C cClass = XmlDeserialize<C>(@"<C><Company>ctrip</Company><B2 sex=""男"" age=""31"" >chinaagan</B2></C>", Encoding.UTF8); B b1 = cClass.b[0]; Console.WriteLine(cClass.Company); Console.WriteLine("sex:" + b1.B_Attribute); Console.WriteLine("age:" + b1.B_Attribute_Age); Console.WriteLine(b1.B_Name); Console.Read(); } public static string XmlSerialize(object o, Encoding encoding) { XmlSerializer serializer = new XmlSerializer(o.GetType()); // StreamWriter 方法 //using (MemoryStream stream = new MemoryStream()) //{ // using (StreamWriter writer = new StreamWriter(stream, encoding)) // { // serializer.Serialize(writer, o); // stream.Position = 0; // using (StreamReader reader = new StreamReader(stream, encoding)) // { // return reader.ReadToEnd(); // } // } //} // XmlWriting方法 using (MemoryStream stream = new MemoryStream()) { using (XmlWriter writer = XmlWriter.Create(stream)) { serializer.Serialize(writer, o); } stream.Position = 0; using (StreamReader reader = new StreamReader(stream, encoding)) { return reader.ReadToEnd(); } } } public static T XmlDeserialize<T>(string s, Encoding encoding) { XmlSerializer mySerializer = new XmlSerializer(typeof(T)); using (MemoryStream ms = new MemoryStream(encoding.GetBytes(s))) { using (StreamReader sr = new StreamReader(ms, encoding)) { return (T)mySerializer.Deserialize(sr); } } } } public class C { public string Company { get; set; } [XmlElement("B2")] public List<B> b { get; set; } } public class B { [XmlAttribute("sex")] public string B_Attribute { get; set; } [XmlAttribute("age")] public string B_Attribute_Age { get; set; } [XmlText] public string B_Name { get; set; } } }
跟上节拍,每日累积一点技术。