#region List<--->DataTable
2 /// <summary>
3 /// DataTable转换泛型集合
4 /// </summary>
5 /// <typeparam name="T"></typeparam>
6 /// <param name="table"></param>
7 /// <returns></returns>
8 public static List<T> DataTableToList<T>(DataTable table)
9 {
10 List<T> list = new List<T>();
11 T t = default(T);
12 PropertyInfo[] propertypes = null;
13 string tempName = string.Empty;
14 foreach (DataRow row in table.Rows)
15 {
16 t = Activator.CreateInstance<T>();
17 propertypes = t.GetType().GetProperties();
18 foreach (PropertyInfo pro in propertypes)
19 {
20 tempName = pro.Name;
21 if (table.Columns.Contains(tempName))
22 {
23 object value = row[tempName];
24 if (!value.ToString().Equals(""))
25 {
26 pro.SetValue(t, value, null);
27 }
28 }
29 }
30 list.Add(t);
31 }
32 return list.Count == 0 ? null : list;
33 }
34
35 /// <summary>
36 /// 将集合类转换成DataTable
37 /// </summary>
38 /// <param name="list">集合</param>
39 /// <returns>DataTable</returns>
40 public static DataTable ListToDataTable(IList list)
41 {
42 DataTable result = new DataTable();
43 if (list != null && list.Count > 0)
44 {
45 PropertyInfo[] propertys = list[0].GetType().GetProperties();
46 foreach (PropertyInfo pi in propertys)
47 {
48 result.Columns.Add(pi.Name, pi.PropertyType);
49 }
50 for (int i = 0; i < list.Count; i++)
51 {
52 ArrayList tempList = new ArrayList();
53 foreach (PropertyInfo pi in propertys)
54 {
55 object obj = pi.GetValue(list[i], null);
56 tempList.Add(obj);
57 }
58 object[] array = tempList.ToArray();
59 result.LoadDataRow(array, true);
60 }
61 }
62 return result;
63 }
64 public static List<T> ConvertTo<T>(DataTable dt) where T : new()
65 {
66 if (dt == null) return null;
67 if (dt.Rows.Count <= 0) return null;
68
69 List<T> list = new List<T>();
70 try
71 {
72 List<string> columnsName = new List<string>();
73 foreach (DataColumn dataColumn in dt.Columns)
74 {
75 columnsName.Add(dataColumn.ColumnName);//得到所有的表头
76 }
77 list = dt.AsEnumerable().ToList().ConvertAll<T>(row => GetObject<T>(row, columnsName)); //转换
78 return list;
79 }
80 catch
81 {
82 return null;
83 }
84 }
85
86 public static T GetObject<T>(DataRow row, List<string> columnsName) where T : new()
87 {
88 T obj = new T();
89 try
90 {
91 string columnname = "";
92 string value = "";
93 PropertyInfo[] Properties = typeof(T).GetProperties();
94 foreach (PropertyInfo objProperty in Properties) //遍历T的属性
95 {
96 columnname = columnsName.Find(name => name.ToLower() == objProperty.Name.ToLower()); //寻找可以匹配的表头名称
97 if (!string.IsNullOrEmpty(columnname))
98 {
99 value = row[columnname].ToString();
100 if (!string.IsNullOrEmpty(value))
101 {
102 if (Nullable.GetUnderlyingType(objProperty.PropertyType) != null) //存在匹配的表头
103 {
104 value = row[columnname].ToString().Replace("$", "").Replace(",", ""); //从dataRow中提取数据
105 objProperty.SetValue(obj, Convert.ChangeType(value, Type.GetType(Nullable.GetUnderlyingType(objProperty.PropertyType).ToString())), null); //赋值操作
106 }
107 else
108 {
109 value = row[columnname].ToString().Replace("%", ""); //存在匹配的表头
110 objProperty.SetValue(obj, Convert.ChangeType(value, Type.GetType(objProperty.PropertyType.ToString())), null);//赋值操作
111 }
112 }
113 }
114 }
115 return obj;
116 }
117 catch
118 {
119 return obj;
120 }
121 }
122 /// <summary>
123 /// 将泛型集合类转换成DataTable
124 /// </summary>
125 /// <typeparam name="T">集合项类型</typeparam>
126 /// <param name="list">集合</param>
127 /// <param name="propertyName">需要返回的列的列名</param>
128 /// <returns>数据集(表)</returns>
129 public static DataTable ListToDataTable<T>(IList<T> list, params string[] propertyName)
130 {
131 List<string> propertyNameList = new List<string>();
132 if (propertyName != null)
133 propertyNameList.AddRange(propertyName);
134 DataTable result = new DataTable();
135 if (list != null && list.Count > 0)
136 {
137 PropertyInfo[] propertys = list[0].GetType().GetProperties();
138 foreach (PropertyInfo pi in propertys)
139 {
140 if (propertyNameList.Count == 0)
141 {
142 result.Columns.Add(pi.Name, pi.PropertyType);
143 }
144 else
145 {
146 if (propertyNameList.Contains(pi.Name))
147 result.Columns.Add(pi.Name, pi.PropertyType);
148 }
149 }
150 for (int i = 0; i < list.Count; i++)
151 {
152 ArrayList tempList = new ArrayList();
153 foreach (PropertyInfo pi in propertys)
154 {
155 if (propertyNameList.Count == 0)
156 {
157 object obj = pi.GetValue(list[i], null);
158 tempList.Add(obj);
159 }
160 else
161 {
162 if (propertyNameList.Contains(pi.Name))
163 {
164 object obj = pi.GetValue(list[i], null);
165 tempList.Add(obj);
166 }
167 }
168 }
169 object[] array = tempList.ToArray();
170 result.LoadDataRow(array, true);
171 }
172 }
173 return result;
174 }
175
176 #endregion
#region 清除HTML标记
2 public static string DropHTML(string Htmlstring)
3 {
4 if (string.IsNullOrEmpty(Htmlstring)) return "";
5 //删除脚本
6 Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
7 //删除HTML
8 Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
9 Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
10 Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
11 Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
12 Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
13 Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
14 Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
15 Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
16 Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
17 Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
18 Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
19 Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
20 Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
21
22 Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);
23 Htmlstring.Replace("<", "");
24 Htmlstring.Replace(">", "");
25 Htmlstring.Replace("\r\n", "");
26 Htmlstring = HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();
27 return Htmlstring;
28 }
29 #endregion