Code
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Xml;
6using System.Xml.Serialization;
7using System.IO;
8
9namespace TaoBao.API.Common
10{
11 /**//// <summary>
12 /// 操作XML常用方法集合
13 /// </summary>
14 public class XMLCommon
15 {
16
17 /**//// <summary>
18 /// 将XML文档转换成实例对象
19 /// </summary>
20 /// <typeparam name="T">对象类型</typeparam>
21 /// <param name="pmFileName">文件名</param>
22 /// <returns>实例对象</returns>
23 public static T DeserializeFile<T>(string pmFileName)
24 {
25 FileStream fs = new FileStream(pmFileName, FileMode.Open);
26 XmlSerializer xs = new XmlSerializer(typeof(T));
27 T tObjext = (T)xs.Deserialize(fs);
28 fs.Close();
29 fs.Dispose();
30 return tObjext;
31 }
32
33 /**//// <summary>
34 /// 将实例对象写入XML文档
35 /// </summary>
36 /// <typeparam name="T">对象类型</typeparam>
37 /// <param name="pmFileName">文件名</param>
38 /// <param name="pmT">实例对象</param>
39 public static void SerializeFile<T>(string pmFileName, T pmT)
40 {
41
42 XmlSerializer serializer = new XmlSerializer(pmT.GetType());
43 TextWriter writer = new StreamWriter(pmFileName);
44 serializer.Serialize(writer, pmT);
45 writer.Close();
46 }
47
48
49 /**//// <summary>
50 /// 将XML字符串转换成对象
51 /// </summary>
52 /// <typeparam name="T">对象类型</typeparam>
53 /// <param name="pmXMLString">XML字符串</param>
54 /// <returns>实例对象</returns>
55 public static T DeserializeXML<T>(string pmXMLString) {
56
57 XmlSerializer xs = new XmlSerializer(typeof(T));
58 T tObjext = (T)xs.Deserialize(new StringReader(pmXMLString));
59 return tObjext;
60 }
61
62 /**//// <summary>
63 /// 将Josn字符串转换成对象
64 /// </summary>
65 /// <typeparam name="T">对象类型</typeparam>
66 /// <param name="pmXMLString">Josn字符串</param>
67 /// <returns>实例对象</returns>
68 public static T DeserializeJSON<T>(string pmXMLString)
69 {
70 XmlSerializer xs = new XmlSerializer(typeof(T));
71 T tObjext = (T)xs.Deserialize(new StringReader(pmXMLString));
72 return tObjext;
73 }
74
75
76 /**//// <summary>
77 /// 将实例对象转换成键值对集合
78 /// </summary>
79 /// <typeparam name="T">对象类型</typeparam>
80 /// <param name="pmT">实例对象</param>
81 /// <returns>键值对集合</returns>
82 public static Dictionary<string,string> DeserializeClass<T>(T pmT)
83 {
84 UTF8Encoding utf8 = new UTF8Encoding();
85 Dictionary<string, string> reValue = new Dictionary<string, string>();
86 string filename = DateTime.Now.ToString("yyyyMMddHHmmssfff")+"xmlfile.xml";
87 SerializeFile(filename, pmT);
88 FileStream fs = new FileStream(filename, FileMode.Open);
89 XmlDocument document = new XmlDocument();
90 document.Load(fs);
91 foreach(XmlNode child in document.DocumentElement.ChildNodes){
92
93 reValue.Add(child.Name, utf8.GetString(utf8.GetBytes(child.InnerText)));
94 }
95 fs.Close();
96 fs.Dispose();
97 //File.Delete(filename);
98 return reValue;
99 }
100
101 /**//// <summary>
102 /// 将键值对集合转成字符串
103 /// </summary>
104 /// <param name="pmDictionary">键值对集合</param>
105 /// <returns>字符串</returns>
106 public static string GetParams(IDictionary<string, string> pmDictionary)
107 {
108 System.Collections.IEnumerator iter = pmDictionary.Keys.GetEnumerator();
109 System.Text.StringBuilder orgin = new System.Text.StringBuilder();
110 int i = 0;
111 while (iter.MoveNext())
112 {
113 string name = (string)iter.Current;
114 string value =System.Web.HttpUtility.UrlEncode(pmDictionary[name], System.Text.Encoding.UTF8);
115 orgin.Append(name).Append("=").Append(value);
116 if (i != pmDictionary.Keys.Count - 1) orgin.Append("&");
117 i++;
118 }
119 return orgin.ToString();
120 }
121
122 }
123}
124
本文转自:http://www.cnblogs.com/bbqqqbq/archive/2009/08/20/1550945.html
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Xml;
6using System.Xml.Serialization;
7using System.IO;
8
9namespace TaoBao.API.Common
10{
11 /**//// <summary>
12 /// 操作XML常用方法集合
13 /// </summary>
14 public class XMLCommon
15 {
16
17 /**//// <summary>
18 /// 将XML文档转换成实例对象
19 /// </summary>
20 /// <typeparam name="T">对象类型</typeparam>
21 /// <param name="pmFileName">文件名</param>
22 /// <returns>实例对象</returns>
23 public static T DeserializeFile<T>(string pmFileName)
24 {
25 FileStream fs = new FileStream(pmFileName, FileMode.Open);
26 XmlSerializer xs = new XmlSerializer(typeof(T));
27 T tObjext = (T)xs.Deserialize(fs);
28 fs.Close();
29 fs.Dispose();
30 return tObjext;
31 }
32
33 /**//// <summary>
34 /// 将实例对象写入XML文档
35 /// </summary>
36 /// <typeparam name="T">对象类型</typeparam>
37 /// <param name="pmFileName">文件名</param>
38 /// <param name="pmT">实例对象</param>
39 public static void SerializeFile<T>(string pmFileName, T pmT)
40 {
41
42 XmlSerializer serializer = new XmlSerializer(pmT.GetType());
43 TextWriter writer = new StreamWriter(pmFileName);
44 serializer.Serialize(writer, pmT);
45 writer.Close();
46 }
47
48
49 /**//// <summary>
50 /// 将XML字符串转换成对象
51 /// </summary>
52 /// <typeparam name="T">对象类型</typeparam>
53 /// <param name="pmXMLString">XML字符串</param>
54 /// <returns>实例对象</returns>
55 public static T DeserializeXML<T>(string pmXMLString) {
56
57 XmlSerializer xs = new XmlSerializer(typeof(T));
58 T tObjext = (T)xs.Deserialize(new StringReader(pmXMLString));
59 return tObjext;
60 }
61
62 /**//// <summary>
63 /// 将Josn字符串转换成对象
64 /// </summary>
65 /// <typeparam name="T">对象类型</typeparam>
66 /// <param name="pmXMLString">Josn字符串</param>
67 /// <returns>实例对象</returns>
68 public static T DeserializeJSON<T>(string pmXMLString)
69 {
70 XmlSerializer xs = new XmlSerializer(typeof(T));
71 T tObjext = (T)xs.Deserialize(new StringReader(pmXMLString));
72 return tObjext;
73 }
74
75
76 /**//// <summary>
77 /// 将实例对象转换成键值对集合
78 /// </summary>
79 /// <typeparam name="T">对象类型</typeparam>
80 /// <param name="pmT">实例对象</param>
81 /// <returns>键值对集合</returns>
82 public static Dictionary<string,string> DeserializeClass<T>(T pmT)
83 {
84 UTF8Encoding utf8 = new UTF8Encoding();
85 Dictionary<string, string> reValue = new Dictionary<string, string>();
86 string filename = DateTime.Now.ToString("yyyyMMddHHmmssfff")+"xmlfile.xml";
87 SerializeFile(filename, pmT);
88 FileStream fs = new FileStream(filename, FileMode.Open);
89 XmlDocument document = new XmlDocument();
90 document.Load(fs);
91 foreach(XmlNode child in document.DocumentElement.ChildNodes){
92
93 reValue.Add(child.Name, utf8.GetString(utf8.GetBytes(child.InnerText)));
94 }
95 fs.Close();
96 fs.Dispose();
97 //File.Delete(filename);
98 return reValue;
99 }
100
101 /**//// <summary>
102 /// 将键值对集合转成字符串
103 /// </summary>
104 /// <param name="pmDictionary">键值对集合</param>
105 /// <returns>字符串</returns>
106 public static string GetParams(IDictionary<string, string> pmDictionary)
107 {
108 System.Collections.IEnumerator iter = pmDictionary.Keys.GetEnumerator();
109 System.Text.StringBuilder orgin = new System.Text.StringBuilder();
110 int i = 0;
111 while (iter.MoveNext())
112 {
113 string name = (string)iter.Current;
114 string value =System.Web.HttpUtility.UrlEncode(pmDictionary[name], System.Text.Encoding.UTF8);
115 orgin.Append(name).Append("=").Append(value);
116 if (i != pmDictionary.Keys.Count - 1) orgin.Append("&");
117 i++;
118 }
119 return orgin.ToString();
120 }
121
122 }
123}
124