把XML格式字符串序列化为对象以及反序列化的静态类

本文内容摘自【Fish Li】[在.net中读写config文件的各种方法] 博文

View Code
  1 using System;
2 using System.Data;
3 using System.Configuration;
4 using System.Text;
5 using System.Xml.Serialization;
6 using System.Xml;
7 using System.IO;
8
9 /// <summary>
10 ///XmlHelper 的摘要说明
11 /// </summary>
12 public static class XmlHelper
13 {
14 /// <summary>
15 /// 将一个对象序列化为XML文档,并返回字节数组
16 /// </summary>
17 /// <param name="o">要序列化的对象</param>
18 /// <param name="encoding">编码方式</param>
19 /// <returns>字节数组</returns>
20 private static byte[] XmlSerializeInternal(object o, Encoding encoding)
21 {
22 if (null == o)
23 throw new ArgumentNullException("o");
24 if (null == encoding)
25 throw new ArgumentNullException("encoding");
26
27 XmlSerializer ser = new XmlSerializer(o.GetType());
28 using (MemoryStream ms = new MemoryStream())
29 {
30 using (XmlTextWriter writer = new XmlTextWriter(ms,encoding))
31 {
32 writer.Formatting = Formatting.Indented;
33 ser.Serialize(writer, o);
34 writer.Close();
35 }
36 return ms.ToArray();
37 }
38 }
39
40 /// <summary>
41 /// 将一个对象序列化为XML字符串
42 /// </summary>
43 /// <param name="o">要序列化的对象</param>
44 /// <param name="encoding">编码方式</param>
45 /// <returns>序列化产生的XML字符串</returns>
46 public static string XmlSerialize(object o,Encoding encoding)
47 {
48 byte[] bytes = XmlSerializeInternal(o, encoding);
49
50 return encoding.GetString(bytes);
51 }
52
53 /// <summary>
54 /// 将一个对象按XML序列化的方式写入到一个文件
55 /// </summary>
56 /// <param name="o">要序列化的对象</param>
57 /// <param name="encoding">编码方式</param>
58 /// <param name="path">保存文件路径</param>
59 public static void XmlSerializeToFile(object o, Encoding encoding, string path)
60 {
61 if (string.IsNullOrEmpty(path))
62 throw new ArgumentNullException("path");
63
64 byte[] bytes = XmlSerializeInternal(o, encoding);
65 File.WriteAllBytes(path, bytes);
66 }
67
68 /// <summary>
69 /// 从XML字符串中反序列化对象
70 /// </summary>
71 /// <typeparam name="T">结果对象类型</typeparam>
72 /// <param name="s">包含对象的XML字符串</param>
73 /// <param name="encoding">编码方式</param>
74 /// <returns>反序列化得到的对象</returns>
75 public static T XmlDeserialize<T>(string s, Encoding encoding)
76 {
77 if (string.IsNullOrEmpty(s))
78 throw new ArgumentNullException("s");
79 if (null == encoding)
80 throw new ArgumentNullException("encoding");
81
82 XmlSerializer ser = new XmlSerializer(typeof(T));
83 using (MemoryStream ms = new MemoryStream(encoding.GetBytes(s)))
84 {
85 using (StreamReader sr = new StreamReader(ms, encoding))
86 {
87 try
88 {
89 return (T)ser.Deserialize(sr);
90 }
91 catch (Exception ex)
92 {
93 throw ex;
94 }
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 (null == encoding)
111 throw new ArgumentNullException("encoding");
112
113 string xml = File.ReadAllText(path);
114 return XmlDeserialize<T>(xml, encoding);
115 }
116 }

注意点:

如果读取已有的XML文件,有一些注意点:
在建MyCommand类时类名必须与XML文件里根节点下的第一个子节点名称一致,public List<MyCommandParameter> Parameters = new List<MyCommandParameter>();这句代码中的Parameters变量命名必须与XML文件中的<Patameters></Patameters>节点一致。
posted @ 2012-01-04 18:18  WILLPAN  阅读(658)  评论(0编辑  收藏  举报