-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathADSXml.cs
263 lines (247 loc) · 9.14 KB
/
ADSXml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using System.Reflection;
using System.Linq.Expressions;
namespace ADS.Utilities.Xml
{
/*2018-10-29 tangdj
* 调用示例
* ADSXml xml = new ADSXml(@"E:\Users\G50-80\Desktop\Config.xml");
Device_Standard d= xml.Get<Device_Standard>();
d.FlowterName = "罗斯蒙特";
xml.Save<Device_Standard>(d);
xml.SaveSingle<Device_Standard>(it=>new Device_Standard {FlowterName="罗斯蒙特" });
[ADSXmlNode("Device_Standard")]
public class Device_Standard
{
public string FlowterName { get; set; }
public string FlowterDes { get; set; }
public string FlowterSN { get; set; }
public string FlowComNo { get; set; }
public string TCPomNo { get; set; }
public string DataBit { get; set; }
public string BaudRate { get; set; }
public string ParityBit { get; set; }
public string StopBit { get; set; }
public string AddressNo { get; set; }
public string ByteOrder { get; set; }
public string Price { get; set; }
public string IsCheck { get; set; }
public string CheckDays { get; set; }
public string RegisterCode { get; set; }
}
* */
/// <summary>
/// ADSXml 的摘要说明。
/// xml操作类
/// </summary>
public class ADSXml
{
protected string strXmlFile;
protected XmlDocument objXmlDoc = new XmlDocument();
/// <summary>
/// xml文本内容
/// </summary>
public Dictionary<string, object> XmlContent = new Dictionary<string, object>();
protected XmlNode NodeCurrent;
protected Dictionary<string, object> DictionaryCurrent;
public ADSXml(string XmlFile)
{
try
{
objXmlDoc.Load(XmlFile);
}
catch (System.Exception ex)
{
throw ex;
}
strXmlFile = XmlFile;
}
/// <summary>
/// 获取Xml结果
/// </summary>
/// <typeparam name="TResult">Model</typeparam>
/// <param name="nodeName">可指定节点名称</param>
/// <returns></returns>
public TResult Get<TResult>(string nodeName=null)
{
getData();
Type type = typeof(TResult);
TResult obj = (TResult)Activator.CreateInstance(type);
PropertyInfo[] propers = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
if (nodeName==null)
{
foreach (var m in type.GetCustomAttributes(true))
{
if (m is ADSXmlNode)
{
ADSXmlNode node = (ADSXmlNode)m;
nodeName = node.NodeName;
}
}
}
if (nodeName==null)
throw new Exception("未指定类的节点特性");
//Dictionary<string, object> dic = (Dictionary<string, object>)XmlContent[nodeName];
findDictionary(XmlContent, nodeName);
if (!DictionaryCurrent.ContainsKey(nodeName))
throw new Exception("Xml文件不存在指定节点");
Dictionary<string, object> dic = (Dictionary<string, object>)DictionaryCurrent[nodeName];
foreach (PropertyInfo p in propers)
{
if (dic.ContainsKey(p.Name))
{
p.SetValue(obj, dic[p.Name], null);
}
}
return obj;
}
/// <summary>
/// 保存单个叶子节点
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="exp"></param>
/// <param name="nodeName"></param>
/// <returns></returns>
public bool SaveSingle<T>(Expression<Func<T, T>> exp,string nodeName=null)
{
Type type = typeof(T);
if (nodeName == null)
{
foreach (var m in type.GetCustomAttributes(true))
{
if (m is ADSXmlNode)
{
ADSXmlNode node = (ADSXmlNode)m;
nodeName = node.NodeName;
}
}
}
if (nodeName == null)
throw new Exception("未指定类的节点特性");
//XmlNode xmlNode = objXmlDoc.SelectSingleNode("Root/Basic/" + nodeName);
findNode(objXmlDoc, nodeName);
if (!NodeCurrent.Name.Equals(nodeName))
return false;
MemberInitExpression body = (MemberInitExpression)exp.Body;
foreach(MemberAssignment item in body.Bindings)
{
var name = item.Member.Name;
ConstantExpression single = (ConstantExpression)item.Expression;
var value = single.Value;
NodeCurrent.SelectSingleNode(name).InnerText = value.ToString();
}
objXmlDoc.Save(strXmlFile);
return true;
}
/// <summary>
/// 以实体类为单位进行保存
/// 对象属性为Null的,不保存
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <param name="nodeName"></param>
/// <returns></returns>
public int Save<T>(T obj, string nodeName = null)
{
int result = 0;
Type type = typeof(T);
PropertyInfo[] propers = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
if (nodeName == null)
{
foreach (var m in type.GetCustomAttributes(true))
{
if (m is ADSXmlNode)
{
ADSXmlNode node = (ADSXmlNode)m;
nodeName = node.NodeName;
}
}
}
if (nodeName == null)
throw new Exception("未指定类的节点特性");
//XmlNode xmlNode= objXmlDoc.SelectSingleNode("Root/Basic/"+nodeName);
findNode(objXmlDoc, nodeName);
if (!NodeCurrent.Name.Equals(nodeName))
return 0;
foreach(PropertyInfo p in propers)
{
var value=p.GetValue(obj, null);
if (value != null)
{
NodeCurrent.SelectSingleNode(p.Name).InnerText = value.ToString();
result++;
}
}
objXmlDoc.Save(strXmlFile);
return result;
}
protected void getData()
{
Dictionary<string, object> dic = XmlContent;
readData(objXmlDoc, dic);
}
protected void findNode(XmlNode xmldoc,string node)
{
XmlNode xml= xmldoc.SelectSingleNode(node);
if (xml == null)
{
foreach (XmlNode m in xmldoc.ChildNodes)
{
findNode(m, node);
}
}
else
{
NodeCurrent = xml;
return;
}
}
protected void findDictionary(Dictionary<string ,object> dic,string key)
{
if (!dic.ContainsKey(key))
{
foreach(var m in dic)
{
try
{
findDictionary((Dictionary<string, object>)m.Value, key);
}
catch { }
}
}
else
{
DictionaryCurrent = dic;
return;
}
}
protected void readData(XmlNode node, Dictionary<string, object> dicValue)
{
XmlNodeList childNode = node.ChildNodes;
try
{
if ((childNode.Count == 1 && !childNode[0].HasChildNodes)||childNode.Count==0)//叶子节点
{
dicValue[node.Name] = node.FirstChild.Value;
return;
}
}
catch
{
return;
}
Dictionary<string, object> dic = new Dictionary<string, object>();
foreach (XmlNode m in childNode)
{
readData(m, dic);
}
dicValue[node.Name] = dic;
}
}
}