之前从网络上找了一个Xml处理帮助类,并整理了一下,这个帮助类针对Object类型进行序列化和反序列化,而不需要提前定义Xml的结构,把它放在这儿供以后使用
1 /// <summary> 2 /// 功能:Xml序列化、反序列化帮助类 3 /// 说明: 4 /// 创建人: 5 /// 创建时间:2014年3月13日 6 /// </summary> 7 public static class XmlHelper 8 { 9 /// <summary> 10 /// 私有方法,不被外部方法访问 11 /// 序列化对象 12 /// </summary> 13 /// <param name="stream">流</param> 14 /// <param name="o">对象</param> 15 /// <param name="encoding">编码方式</param> 16 private static void XmlSerializeInternal(Stream stream, object o, Encoding encoding) 17 { 18 if (o == null) 19 throw new ArgumentNullException("o"); 20 if (encoding == null) 21 throw new ArgumentNullException("encoding"); 22 23 XmlSerializer serializer = new XmlSerializer(o.GetType()); 24 25 XmlWriterSettings settings = new XmlWriterSettings(); 26 settings.Indent = true; 27 settings.NewLineChars = "\r\n"; 28 settings.Encoding = encoding; 29 settings.IndentChars = " "; 30 31 using (XmlWriter writer = XmlWriter.Create(stream, settings)) 32 { 33 serializer.Serialize(writer, o); 34 writer.Close(); 35 } 36 } 37 38 /// <summary> 39 /// 序列化,将一个对象序列化为XML字符串 40 /// </summary> 41 /// <param name="o">要序列化的对象</param> 42 /// <param name="encoding">编码方式</param> 43 /// <returns>序列化产生的XML字符串</returns> 44 public static string XmlSerialize(object o, Encoding encoding) 45 { 46 using (MemoryStream stream = new MemoryStream()) 47 { 48 XmlSerializeInternal(stream, o, encoding); 49 50 stream.Position = 0; 51 using (StreamReader reader = new StreamReader(stream, encoding)) 52 { 53 return reader.ReadToEnd(); 54 } 55 } 56 } 57 58 /// <summary> 59 /// 序列化,将一个对象按XML序列化的方式写入到一个文件 60 /// </summary> 61 /// <param name="o">要序列化的对象</param> 62 /// <param name="path">保存文件路径</param> 63 /// <param name="encoding">编码方式</param> 64 public static void XmlSerializeToFile(object o, string path, Encoding encoding) 65 { 66 if (string.IsNullOrEmpty(path)) 67 throw new ArgumentNullException("path"); 68 69 using (FileStream file = new FileStream(path, FileMode.Create, FileAccess.Write)) 70 { 71 XmlSerializeInternal(file, o, encoding); 72 } 73 } 74 75 /// <summary> 76 /// 反序列化,从XML字符串中反序列化对象 77 /// </summary> 78 /// <typeparam name="T">结果对象类型</typeparam> 79 /// <param name="s">包含对象的XML字符串</param> 80 /// <param name="encoding">编码方式</param> 81 /// <returns>反序列化得到的对象</returns> 82 public static T XmlDeserialize<T>(string s, Encoding encoding) 83 { 84 if (string.IsNullOrEmpty(s)) 85 throw new ArgumentNullException("s"); 86 if (encoding == null) 87 throw new ArgumentNullException("encoding"); 88 89 XmlSerializer mySerializer = new XmlSerializer(typeof(T)); 90 using (MemoryStream ms = new MemoryStream(encoding.GetBytes(s))) 91 { 92 using (StreamReader sr = new StreamReader(ms, encoding)) 93 { 94 return (T)mySerializer.Deserialize(sr); 95 } 96 } 97 } 98 99 /// <summary> 100 /// 反序列化,读入一个文件,并按XML的方式反序列化对象。 101 /// </summary> 102 /// <typeparam name="T">结果对象类型</typeparam> 103 /// <param name="path">文件路径</param> 104 /// <param name="encoding">编码方式</param> 105 /// <returns>反序列化得到的对象</returns> 106 public static T XmlDeserializeFromFile<T>(string path, Encoding encoding) 107 { 108 if (string.IsNullOrEmpty(path)) 109 throw new ArgumentNullException("path"); 110 if (encoding == null) 111 throw new ArgumentNullException("encoding"); 112 113 string xml = File.ReadAllText(path, encoding); 114 return XmlDeserialize<T>(xml, encoding); 115 } 116 }
时间: 2024-10-11 06:39:44