xml文件net操纵类(c#)
1
using System;
2
using System.Xml;
3
using System.Web;
4
namespace MX.XML
5
{
6
/// <summary>
7
/// XML核心类:
8
/// 必需用XPath表达式来获取相应节点
9
/// </summary>
10
public class Core
11
{
12
#region 变量-----------------------------------------------------------
13
/// <summary>
14
/// xml文件所在路径类型
15
/// </summary>
16
/// <remarks>xml文件所在路径类型</remarks>
17
public enum enumXmlPathType
18
{
19
/// <summary>
20
/// 绝对路径
21
/// </summary>
22
AbsolutePath,
23
/// <summary>
24
/// 虚拟路径
25
/// </summary>
26
VirtualPath
27
}
28
29
private string xmlFilePath;
30
private enumXmlPathType xmlFilePathType;
31
private XmlDocument xmlDoc = new XmlDocument();
32
#endregion
33
34
属性-----------------------------------------------------------
63
64
构造函数-------------------------------------------------------
120
121
获取所有指定名称的节点
146
147
读取指定节点的指定属性值---------------------------------------
185
186
读取指定节点的值-----------------------------------------------
210
211
设置节点值-----------------------------------------------------
245
246
#region 设置节点的属性值-----------------------------------------------
247
/// <summary>
248
/// 功能:
249
/// 设置节点的属性值
250
/// </summary>
251
/// <param >节点名称</param>
252
/// <param >属性名称</param>
253
/// <param >属性值</param>
254
public void SetXmlNodeAttributeValue(string xmlNodePath, string xmlNodeAttribute, string xmlNodeAttributeValue)
255
{
256
try
257
{
258
//可以批量为符合条件的节点的属性付值
259
XmlNodeList xmlNode = this.xmlDoc.SelectNodes(xmlNodePath);
260
if (!(xmlNode == null))
261
{
262
foreach (XmlNode xn in xmlNode)
263
{
264
XmlAttributeCollection xmlAttr = xn.Attributes;
265
for (int i = 0; i < xmlAttr.Count; i++)
266
{
267
if (xmlAttr.Item(i).Name == xmlNodeAttribute)
268
{
269
xmlAttr.Item(i).Value = xmlNodeAttributeValue;
270
break;
271
}
272
}
273
}
274
}
275
}
276
catch (XmlException xmle)
277
{
278
throw xmle;
279
}
280
}
281
#endregion
282
283
#region 添加-----------------------------------------------------------
284
/// <summary>
285
/// 获取XML文件的根元素
286
/// </summary>
287
public XmlNode GetXmlRoot()
288
{
289
return xmlDoc.DocumentElement;
290
}
291
292
/// <summary>
293
/// 在根节点下添加父节点
294
/// </summary>
295
public void AddParentNode(string parentNode)
296
{
297
try
298
{
299
XmlNode root = GetXmlRoot();
300
XmlNode parentXmlNode = xmlDoc.CreateElement(parentNode);
301
root.AppendChild(parentXmlNode);
302
}
303
catch (XmlException xmle)
304
{
305
throw xmle;
306
}
307
}
308
309
/// <summary>
310
/// 向一个已经存在的父节点中插入一个子节点,并返回子节点.
311
/// </summary>
312
/// <param >父节点</param>
313
/// <param >字节点名称</param>
314
public XmlNode AddChildNode(string parentNodePath, string childnodename)
315
{
316
XmlNode childXmlNode = null;
317
try
318
{
319
XmlNode parentXmlNode = xmlDoc.SelectSingleNode(parentNodePath);
320
if (!((parentXmlNode) == null))//如果此节点存在
321
{
322
childXmlNode = xmlDoc.CreateElement(childnodename);
323
parentXmlNode.AppendChild(childXmlNode);
324
}
325
else
326
{//如果不存在就放父节点添加
327
this.GetXmlRoot().AppendChild(childXmlNode);
328
}
329
}
330
catch (XmlException xmle)
331
{
332
throw xmle;
333
}
334
return childXmlNode;
335
}
336
/// <summary>
337
/// 向一个已经存在的父节点中插入一个子节点,并添加一个属性
338
/// </summary>
339
public void AddChildNode(string parentNodePath, string childnodename, string NodeAttribute, string NodeAttributeValue)
340
{
341
try
342
{
343
XmlNode parentXmlNode = xmlDoc.SelectSingleNode(parentNodePath);
344
XmlNode childXmlNode = null;
345
if (!((parentXmlNode) == null))//如果此节点存在
346
{
347
childXmlNode = xmlDoc.CreateElement(childnodename);
348
349
//添加属性
350
XmlAttribute nodeAttribute = this.xmlDoc.CreateAttribute(NodeAttribute);
351
nodeAttribute.Value = NodeAttributeValue;
352
childXmlNode.Attributes.Append(nodeAttribute);
353
354
parentXmlNode.AppendChild(childXmlNode);
355
}
356
else
357
{//如果不存在就放父节点添加
358
this.GetXmlRoot().AppendChild(childXmlNode);
359
}
360
}
361
catch (XmlException xmle)
362
{
363
throw xmle;
364
}
365
}
366
367
/// <summary>
368
/// 向一个节点添加属性,值为空
369
/// </summary>
370
/// <param >节点路径</param>
371
/// <param >属性名</param>
372
public void AddAttribute(string NodePath, string NodeAttribute)
373
{
374
privateAddAttribute(NodePath, NodeAttribute, "");
375
}
376
/// <summary>
377
/// 向一个节点添加属性,并赋值***
378
/// </summary>
379
public void AddAttribute(XmlNode childXmlNode, string NodeAttribute, string NodeAttributeValue)
380
{
381
XmlAttribute nodeAttribute = this.xmlDoc.CreateAttribute(NodeAttribute);
382
nodeAttribute.Value = NodeAttributeValue;
383
childXmlNode.Attributes.Append(nodeAttribute);
384
}
385
386
/// <summary>
387
/// 向一个节点添加属性
388
/// </summary>
389
/// <param >节点路径</param>
390
/// <param >属性名</param>
391
/// <param >属性值</param>
392
private void privateAddAttribute(string NodePath, string NodeAttribute, string NodeAttributeValue)
393
{
394
try
395
{
396
XmlNode nodePath = xmlDoc.SelectSingleNode(NodePath);
397
if (!(nodePath == null))
398
{
399
XmlAttribute nodeAttribute = this.xmlDoc.CreateAttribute(NodeAttribute);
400
nodeAttribute.Value = NodeAttributeValue;
401
nodePath.Attributes.Append(nodeAttribute);
402
}
403
}
404
catch (XmlException xmle)
405
{
406
throw xmle;
407
}
408
}
409
/// <summary>
410
/// 向一个节点添加属性,并赋值
411
/// </summary>
412
/// <param >节点</param>
413
/// <param >属性名</param>
414
/// <param >属性值</param>
415
public void AddAttribute(string NodePath, string NodeAttribute, string NodeAttributeValue)
416
{
417
privateAddAttribute(NodePath, NodeAttribute, NodeAttributeValue);
418
}
419
#endregion
420
421
删除-----------------------------------------------------------
490
491
XML文档事件----------------------------------------------------
523
524
保存XML文件----------------------------------------------------
575
576
}
577
578
}
using System;2
using System.Xml;3
using System.Web;4
namespace MX.XML5
{6
/// <summary>7
/// XML核心类:8
/// 必需用XPath表达式来获取相应节点9
/// </summary>10
public class Core11
{12
#region 变量-----------------------------------------------------------13
/// <summary>14
/// xml文件所在路径类型15
/// </summary>16
/// <remarks>xml文件所在路径类型</remarks>17
public enum enumXmlPathType18
{19
/// <summary>20
/// 绝对路径21
/// </summary>22
AbsolutePath,23
/// <summary>24
/// 虚拟路径25
/// </summary>26
VirtualPath27
}28

29
private string xmlFilePath;30
private enumXmlPathType xmlFilePathType;31
private XmlDocument xmlDoc = new XmlDocument();32
#endregion33

34
属性-----------------------------------------------------------63

64
构造函数-------------------------------------------------------120

121
获取所有指定名称的节点146

147
读取指定节点的指定属性值---------------------------------------185

186
读取指定节点的值-----------------------------------------------210

211
设置节点值-----------------------------------------------------245

246
#region 设置节点的属性值-----------------------------------------------247
/// <summary>248
/// 功能:249
/// 设置节点的属性值 250
/// </summary>251
/// <param >节点名称</param>252
/// <param >属性名称</param>253
/// <param >属性值</param>254
public void SetXmlNodeAttributeValue(string xmlNodePath, string xmlNodeAttribute, string xmlNodeAttributeValue)255
{256
try257
{258
//可以批量为符合条件的节点的属性付值259
XmlNodeList xmlNode = this.xmlDoc.SelectNodes(xmlNodePath);260
if (!(xmlNode == null))261
{262
foreach (XmlNode xn in xmlNode)263
{264
XmlAttributeCollection xmlAttr = xn.Attributes;265
for (int i = 0; i < xmlAttr.Count; i++)266
{267
if (xmlAttr.Item(i).Name == xmlNodeAttribute)268
{269
xmlAttr.Item(i).Value = xmlNodeAttributeValue;270
break;271
}272
}273
}274
}275
}276
catch (XmlException xmle)277
{278
throw xmle;279
}280
}281
#endregion282

283
#region 添加-----------------------------------------------------------284
/// <summary>285
/// 获取XML文件的根元素286
/// </summary>287
public XmlNode GetXmlRoot()288
{289
return xmlDoc.DocumentElement;290
}291

292
/// <summary>293
/// 在根节点下添加父节点294
/// </summary>295
public void AddParentNode(string parentNode)296
{297
try298
{299
XmlNode root = GetXmlRoot();300
XmlNode parentXmlNode = xmlDoc.CreateElement(parentNode);301
root.AppendChild(parentXmlNode);302
}303
catch (XmlException xmle)304
{305
throw xmle;306
}307
}308

309
/// <summary>310
/// 向一个已经存在的父节点中插入一个子节点,并返回子节点.311
/// </summary>312
/// <param >父节点</param>313
/// <param >字节点名称</param>314
public XmlNode AddChildNode(string parentNodePath, string childnodename)315
{316
XmlNode childXmlNode = null;317
try318
{319
XmlNode parentXmlNode = xmlDoc.SelectSingleNode(parentNodePath);320
if (!((parentXmlNode) == null))//如果此节点存在321
{322
childXmlNode = xmlDoc.CreateElement(childnodename);323
parentXmlNode.AppendChild(childXmlNode);324
}325
else326
{//如果不存在就放父节点添加327
this.GetXmlRoot().AppendChild(childXmlNode);328
}329
}330
catch (XmlException xmle)331
{332
throw xmle;333
}334
return childXmlNode;335
}336
/// <summary>337
/// 向一个已经存在的父节点中插入一个子节点,并添加一个属性338
/// </summary>339
public void AddChildNode(string parentNodePath, string childnodename, string NodeAttribute, string NodeAttributeValue)340
{341
try342
{343
XmlNode parentXmlNode = xmlDoc.SelectSingleNode(parentNodePath);344
XmlNode childXmlNode = null;345
if (!((parentXmlNode) == null))//如果此节点存在346
{347
childXmlNode = xmlDoc.CreateElement(childnodename);348

349
//添加属性350
XmlAttribute nodeAttribute = this.xmlDoc.CreateAttribute(NodeAttribute);351
nodeAttribute.Value = NodeAttributeValue;352
childXmlNode.Attributes.Append(nodeAttribute);353

354
parentXmlNode.AppendChild(childXmlNode);355
}356
else357
{//如果不存在就放父节点添加358
this.GetXmlRoot().AppendChild(childXmlNode);359
}360
}361
catch (XmlException xmle)362
{363
throw xmle;364
}365
}366

367
/// <summary>368
/// 向一个节点添加属性,值为空369
/// </summary>370
/// <param >节点路径</param>371
/// <param >属性名</param>372
public void AddAttribute(string NodePath, string NodeAttribute)373
{374
privateAddAttribute(NodePath, NodeAttribute, "");375
}376
/// <summary>377
/// 向一个节点添加属性,并赋值***378
/// </summary>379
public void AddAttribute(XmlNode childXmlNode, string NodeAttribute, string NodeAttributeValue)380
{381
XmlAttribute nodeAttribute = this.xmlDoc.CreateAttribute(NodeAttribute);382
nodeAttribute.Value = NodeAttributeValue;383
childXmlNode.Attributes.Append(nodeAttribute);384
}385

386
/// <summary>387
/// 向一个节点添加属性388
/// </summary>389
/// <param >节点路径</param>390
/// <param >属性名</param>391
/// <param >属性值</param>392
private void privateAddAttribute(string NodePath, string NodeAttribute, string NodeAttributeValue)393
{394
try395
{396
XmlNode nodePath = xmlDoc.SelectSingleNode(NodePath);397
if (!(nodePath == null))398
{399
XmlAttribute nodeAttribute = this.xmlDoc.CreateAttribute(NodeAttribute);400
nodeAttribute.Value = NodeAttributeValue;401
nodePath.Attributes.Append(nodeAttribute);402
}403
}404
catch (XmlException xmle)405
{406
throw xmle;407
}408
}409
/// <summary>410
/// 向一个节点添加属性,并赋值411
/// </summary>412
/// <param >节点</param>413
/// <param >属性名</param>414
/// <param >属性值</param>415
public void AddAttribute(string NodePath, string NodeAttribute, string NodeAttributeValue)416
{417
privateAddAttribute(NodePath, NodeAttribute, NodeAttributeValue);418
}419
#endregion420

421
删除-----------------------------------------------------------490

491
XML文档事件----------------------------------------------------523

524
保存XML文件----------------------------------------------------575

576
}577

578
}


浙公网安备 33010602011771号