修改的XML操作类
最近,有个项目需要用到XML,就在博客园搜了一下,找了一篇比较完整、功能比较齐全,但是在使用的过程并没有满足项目要求,于是就做了响应的修改!贴出来让大家看看!!
类代码如下:
1
using System;2
using System.Collections.Generic;3
using System.Text;4
using System.Xml;5
using System.Xml.Xsl;6
using System.Web;7
using System.Data;8
namespace DAL.WorkFlow_Document9


{10

/**//// <summary>11
/// XML核心类12
/// 必需用XPath表达式来获取相应节点13
/// </summary>14
public class docOperate15

{16

变量#region 变量17

/**//// <summary>18
/// xml文件所在路径类型19
/// </summary>20
/// <remarks>xml文件所在路径类型</remarks>21
public enum enumXmlPathType22

{23

/**//**/24

/**//// <summary>25
/// 绝对路径26
/// </summary>27
AbsolutePath,28

/**//**/29

/**//// <summary>30
/// 虚拟路径31
/// </summary>32
VirtualPath33
}34
private string xmlFilePath;35
enumXmlPathType xmlFilePathType;36
XmlDataDocument xmlDoc = new XmlDataDocument();37
#endregion38

39

属性#region 属性40

/**//// <summary>41
/// 文件路径42
/// </summary>43
/// <remarks>文件路径</remarks>44
public string XmlFilePath45

{46

set
{ xmlFilePath = value; }47

get
{ return xmlFilePath; }48
}49

/**//// <summary>50
/// 文件路径类型51
/// </summary>52
public enumXmlPathType XmlFilePathType53

{54

get
{ return xmlFilePathType; }55
}56
#endregion57

58

构造函数#region 构造函数59

/**//// <summary>60
/// 构造函数61
/// </summary>62
/// <param name="tempXmlFilePath">文件路径</param>63
public docOperate(string tempXmlFilePath)64

{65
this.xmlFilePathType = enumXmlPathType.VirtualPath;66
this.XmlFilePath = tempXmlFilePath;67
}68

/**//// <summary>69
/// 构造函数70
/// </summary>71
/// <param name="tempXmlFilePath">文件路径</param>72
/// <param name="tempXmlFilePathType">类型</param>73
public docOperate(string tempXmlFilePath, enumXmlPathType tempXmlFilePathType)74

{75
this.xmlFilePathType = tempXmlFilePathType;76
this.XmlFilePath = tempXmlFilePath;77
}78

/**//// <summary>79
/// 获取XmlDocument实体类80
/// </summary>81
/// <returns>指定的XML描述文件的一个xmldocument实例</returns>82
private XmlDataDocument GetXmlDocument()83

{84
XmlDataDocument doc = null;85
if (this.xmlFilePathType == enumXmlPathType.AbsolutePath)86

{87
doc = GetXmlDocumentFromFile(XmlFilePath);88
}89
else90
if (this.xmlFilePathType == enumXmlPathType.VirtualPath)91

{92
doc = GetXmlDocumentFromFile(HttpContext.Current.Server.MapPath(XmlFilePath));93
}94
return doc;95
}96

/**//// <summary>97
/// 98
/// </summary>99
/// <param name="tempXmlFilePath"></param>100
/// <returns></returns>101
private XmlDataDocument GetXmlDocumentFromFile(string tempXmlFilePath)102

{103
string xmlFileFullPath = tempXmlFilePath;104
xmlDoc.Load(xmlFileFullPath);105
xmlDoc.NodeChanged += new XmlNodeChangedEventHandler(this.nodeUpdateEvent);106
xmlDoc.NodeInserted += new XmlNodeChangedEventHandler(this.nodeInsertEvent);107
xmlDoc.NodeRemoved += new XmlNodeChangedEventHandler(this.nodeDeleteEvent);108
return xmlDoc;109
}110
#endregion111

112

获取所有指定名称的节点#region 获取所有指定名称的节点 113

/**//// <summary>114
/// 功能:获取所有指定名称的节点(XmlNodeList)115
/// </summary>116
/// <param name="strNode">节点名称</param>117
/// <returns></returns>118
public XmlNodeList GetXmlNodeList(string strNode)119

{120
XmlNodeList strReturn = null;121
try 122

{123
XmlNodeList xmlNode = xmlDoc.SelectNodes(strNode);124
if (xmlNode != null)125

{126
strReturn = xmlNode;127
}128
}129
catch(XmlException xmlEx) 130

{131
throw xmlEx;132
}133
return strReturn;134
}135
#endregion136

137

读取指定节点的指定属性值#region 读取指定节点的指定属性值138

/**//// <summary>139
/// 读取指定节点的指定属性值(Value)140
/// </summary>141
/// <param name="strNode">节点名称</param>142
/// <param name="strAttribute">此节点的属性</param>143
/// <returns></returns>144
public string GetXmlNodeAttributeValue(string strNode, string strAttribute)145

{146
string strReturn = "";147
try148

{149
//根据指定路径获取节点150
XmlNode xmlNode = xmlDoc.SelectSingleNode(strNode);151
if (!(xmlNode == null))152

{153
strReturn = xmlNode.Attributes.GetNamedItem(strAttribute).Value;154

/**//**/155

/**/////获取节点的属性,并循环取出需要的属性值156
//XmlAttributeCollection xmlAttr = xmlNode.Attributes;157
//for (int i = 0; i < xmlAttr.Count; i++)158
//{159
// if (xmlAttr.Item(i).Name == strAttribute)160
// {161
// strReturn = xmlAttr.Item(i).Value;162
// }163
//}164
}165
}166
catch (XmlException xmle)167

{168
throw xmle;169
}170
return strReturn;171
}172
#endregion173

174

读取指定节点的值#region 读取指定节点的值175

/**//// <summary>176
/// 读取指定节点的值(InnerText)177
/// </summary>178
/// <param name="strNode">节点名称</param>179
/// <returns></returns>180
public string GetXmlNodeValue(string strNode)181

{182
string strReturn = String.Empty;183
try 184

{185
XmlNode xmlNode = xmlDoc.SelectSingleNode(strNode);186
//XmlNode xmlnode=xm187
if (xmlNode != null)188

{189
strReturn = xmlNode.InnerXml;190
}191
}192
catch (XmlException xmle)193

{194
throw xmle; 195
}196
return strReturn;197
}198
#endregion199

200

设置节点值#region 设置节点值201

/**//// <summary>202
/// 设置节点值(InnerText)203
/// </summary>204
/// <param name="xmlNodePath">节点的名称</param>205
/// <param name="xmlNodeValue">节点值</param>206
public void SetXmlNodeValue(string xmlNodePath, string xmlNodeValue)207

{208
try209

{210
//可以批量为符合条件的节点进行付值211
XmlNodeList xmlNode = this.xmlDoc.SelectNodes(xmlNodePath);212
//213
if (!(xmlNode == null))214

{215
if (xmlNode.Count == 1)216

{217
foreach (XmlNode xn in xmlNode)218

{219
xn.InnerText = xmlNodeValue;220
}221
}222
else223
if (xmlNode.Count > 1)224

{225
int count = 1;226
foreach (XmlNode xn in xmlNode)227

{228
if (xmlNode.Count == count)229

{230
xn.InnerText = xmlNodeValue;231
}232
count++;233
}234
}235
}236

237

/**///// 根据指定路径获取节点 238
//XmlNode xmlNode = xmlDoc.SelectSingleNode(xmlNodePath) ; 239

/**/////设置节点值240
//if (!(xmlNode == null))241
// xmlNode.InnerText = xmlNodeValue;242
}243
catch (XmlException xmle)244

{245
throw xmle;246
}247
}248
#endregion249

250

设置节点的属性值#region 设置节点的属性值251

/**//// <summary>252
/// 设置节点的属性值 253
/// </summary>254
/// <param name="xmlNodePath">节点名称</param>255
/// <param name="xmlNodeAttribute">属性名称</param>256
/// <param name="xmlNodeAttributeValue">属性值</param>257
public void SetXmlNodeAttributeValue(string xmlNodePath, string xmlNodeAttribute, string xmlNodeAttributeValue)258

{259
try260

{261
//可以批量为符合条件的节点的属性赋值262
XmlNodeList xmlNode = this.xmlDoc.SelectNodes(xmlNodePath);263
if (!(xmlNode == null))264

{265
if (xmlNode.Count == 1)266

{267
foreach (XmlNode xn in xmlNode)268

{269
XmlAttributeCollection xmlAttr = xn.Attributes;270
for (int i = 0; i < xmlAttr.Count; i++)271

{272
if (xmlAttr.Item(i).Name == xmlNodeAttribute)273

{274
xmlAttr.Item(i).Value = xmlNodeAttributeValue;275
break;276
}277
}278
}279
}280
else281
if (xmlNode.Count > 1)282

{283
int count = 1;284
foreach (XmlNode xn in xmlNode)285

{286
if (xmlNode.Count == count)287

{288
XmlAttributeCollection xmlAttr = xn.Attributes;289
for (int i = 0; i < xmlAttr.Count; i++)290

{291
if (xmlAttr.Item(i).Name == xmlNodeAttribute)292

{293
xmlAttr.Item(i).Value = xmlNodeAttributeValue;294
break;295
}296
}297
}298
count++;299
}300
}301
}302
}303
catch (XmlException xmle)304

{305
throw xmle;306
}307
}308
#endregion309

310

添加#region 添加311

/**//// <summary>312
/// 给XML文件添加根元素313
/// </summary>314
/// <param name="nodeValue">根元素</param>315
public void AddXmlRootNode(string nodeValue)316

{317
XmlDeclaration xn = xmlDoc.CreateXmlDeclaration("1.0","gb2312",null);318
xmlDoc.AppendChild(xn);319
//XslCompiledTransform xc=xmlDoc.x320
//XmlElement xmlRootNode = xmlDoc.CreateElement("",nodeValue,"");type="text/xsl" href=\"XmlDocStyle.xsl\"321
XmlProcessingInstruction xp = xmlDoc.CreateProcessingInstruction("xml-stylesheet", "type=\"text/xsl\"" + " " + "href=\"XmlDoc.xsl\"");322
xmlDoc.AppendChild(xp);323
XmlElement xmlRootNode = xmlDoc.CreateElement(nodeValue);324
xmlRootNode.SetAttribute("xmlns", "http://www.egs.org.cn/eGovDoc/body");325
xmlRootNode.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XM1.Schema-instance");326
xmlDoc.AppendChild(xmlRootNode);327
}328

/**//// <summary>329
/// 获取XML文件的根元素330
/// </summary>331
/// <returns></returns>332
public XmlNode GetXmlRoot()333

{334
return xmlDoc.DocumentElement;335
}336

/**//// <summary>337
/// 在根节点下添加父节点338
/// </summary>339
/// <param name="parentNode"></param>340
public void AddParentNode(string parentNode)341

{342
try343

{344
XmlNode root = GetXmlRoot();345
XmlNode parentXmlNode = xmlDoc.CreateElement(parentNode);346
root.AppendChild(parentXmlNode);347
}348
catch (XmlException xmle)349

{350
throw xmle;351
}352
}353

/**//// <summary>354
/// 向一个已经存在的父节点中插入一个子节点,并返回子节点.355
/// </summary>356
/// <param name="parentNodePath">父节点</param>357
/// <param name="childnodename">字节点名称</param>358
/// <returns></returns>359
public XmlNode AddChildNode(string parentNodePath, string childnodename)360

{361
XmlNode childXmlNode =null;362
try363

{364
XmlNode parentXmlNode = xmlDoc.SelectSingleNode(parentNodePath);365
if (parentXmlNode != null)366

{367
childXmlNode = xmlDoc.CreateElement(childnodename);368
parentXmlNode.AppendChild(childXmlNode);369
}370
else371

{372
this.GetXmlRoot().AppendChild(childXmlNode);373
}374
}375
catch (XmlException xmle)376

{377
throw xmle;378
}379
return childXmlNode;380
}381

/**//// <summary>382
/// 向一个已经存在的父节点中插入一个子节点,并添加一个属性383
/// </summary>384
/// <param name="parentNodePath"></param>385
/// <param name="childnodename"></param>386
/// <param name="NodeAttribute"></param>387
/// <param name="NodeAttributeValue"></param>388
public void AddChildNode(string parentNodePath, string childnodename, string NodeAttribute, string NodeAttributeValue)389

{390
try391

{392
XmlNode parentXmlNode = xmlDoc.SelectSingleNode(parentNodePath);393
XmlNode childXmlNode = null;394
if (!(parentXmlNode == null))395

{396
childXmlNode = xmlDoc.CreateElement(childnodename);397
//添加属性398
XmlAttribute nodeAttribute = this.xmlDoc.CreateAttribute(NodeAttribute);399
nodeAttribute.Value = NodeAttributeValue;400
childXmlNode.Attributes.Append(nodeAttribute);401

402
parentXmlNode.AppendChild(childXmlNode);403
}404
else405

{406
this.GetXmlRoot().AppendChild(childXmlNode);407
}408
}409
catch (XmlException xmle)410

{411
throw xmle;412
}413
}414

/**//// <summary>415
/// 向一个节点添加属性,值为空416
/// </summary>417
/// <param name="NodePath">节点路径</param>418
/// <param name="NodeAttribute">属性名</param>419
public void AddAttribute(string NodePath, string NodeAttribute)420

{421
privateAddAttribute(NodePath, NodeAttribute, "");422
}423

/**//// <summary>424
/// 向一个节点添加属性,并赋值***425
/// </summary>426
/// <param name="childXmlNode"></param>427
/// <param name="NodeAttribute"></param>428
/// <param name="NodeAttributeValue"></param>429
public void AddAttribute(XmlNode childXmlNode, string NodeAttribute, string NodeAttributeValue)430

{431
XmlAttribute nodeAttribute = this.xmlDoc.CreateAttribute(NodeAttribute);432
nodeAttribute.Value = NodeAttributeValue;433
childXmlNode.Attributes.Append(nodeAttribute);434
}435

/**//// <summary>436
/// 向一个节点添加属性437
/// </summary>438
/// <param name="NodePath">节点路径</param>439
/// <param name="NodeAttribute">属性名</param>440
/// <param name="NodeAttributeValue">属性值</param>441
private void privateAddAttribute(string NodePath, string NodeAttribute, string NodeAttributeValue)442

{443
try 444

{445
//XmlNode nodePath = xmlDoc.SelectSingleNode(NodePath);446
//if (!(nodePath == null))447
//{448
// XmlAttribute nodeAttribute = this.xmlDoc.CreateAttribute(NodeAttribute);449
// nodeAttribute.Value = NodeAttributeValue;450
// nodePath.Attributes.Append(nodeAttribute);451
//}452
XmlNodeList nodePath = xmlDoc.SelectNodes(NodePath);453
if (!(nodePath == null))454

{455
if (nodePath.Count == 1)456

{457
foreach (XmlNode xn in nodePath)458

{459
XmlAttribute nodeAttribute = this.xmlDoc.CreateAttribute(NodeAttribute);460
nodeAttribute.Value = NodeAttributeValue;461
xn.Attributes.Append(nodeAttribute);462
}463
}464
else465
if (nodePath.Count > 1)466

{467
int count = 1;468
foreach (XmlNode xn in nodePath)469

{470
if (nodePath.Count == count)471

{472
XmlAttribute nodeAttribute = this.xmlDoc.CreateAttribute(NodeAttribute);473
nodeAttribute.Value = NodeAttributeValue;474
xn.Attributes.Append(nodeAttribute);475
}476
count++;477
}478
}479
}480
}481
catch (XmlException xmle)482

{483
throw xmle;484
}485
}486

/**//// <summary>487
/// 向一个节点添加属性,并赋值488
/// </summary>489
/// <param name="NodePath">节点</param>490
/// <param name="NodeAttribute">属性名</param>491
/// <param name="NodeAttributeValue">属性值</param>492
public void AddAttribute(string NodePath, string NodeAttribute, string NodeAttributeValue)493

{494
privateAddAttribute(NodePath, NodeAttribute, NodeAttributeValue);495
//SetXmlNodeAttributeValue(NodePath, NodeAttribute, NodeAttributeValue);496
}497
#endregion498

499

删除#region 删除500

/**//// <summary>501
/// 删除节点的一个属性502
/// </summary>503
/// <param name="NodePath">节点所在的xpath表达式</param>504
/// <param name="NodeAttribute">属性名</param>505
public void DeleteAttribute(string NodePath, string NodeAttribute)506

{507
XmlNodeList nodePath = this.xmlDoc.SelectNodes(NodePath);508
if (!(nodePath == null))509

{510
foreach (XmlNode tempxn in nodePath)511

{512
XmlAttributeCollection xmlAtrr = tempxn.Attributes;513
for (int i = 0; i < xmlAtrr.Count; i++)514

{515
if (xmlAtrr.Item(i).Name == NodeAttribute)516

{517
tempxn.Attributes.RemoveAt(i);518
break;519
}520
}521
}522
}523
}524

/**//// <summary>525
/// 删除节点的一个属性,当其属性值等于给定的值时526
/// </summary>527
/// <param name="NodePath">节点所在的xpath表达式</param>528
/// <param name="NodeAttribute">属性</param>529
/// <param name="NodeAttributeValue">值</param>530
public void DeleteAttribute(string NodePath, string NodeAttribute, string NodeAttributeValue)531

{532
XmlNodeList nodePath = this.xmlDoc.SelectNodes(NodePath);533
if (!(nodePath == null))534

{535
foreach (XmlNode tempxn in nodePath)536

{537
XmlAttributeCollection xmlAttr = tempxn.Attributes;538
for (int i = 0; i < xmlAttr.Count; i++)539

{540
if (xmlAttr.Item(i).Name == NodeAttribute && xmlAttr.Item(i).Value==NodeAttributeValue)541

{542
tempxn.Attributes.RemoveAt(i);543
break;544
}545
}546
}547
}548
}549

/**//// <summary>550
/// 删除节点551
/// </summary>552
/// <param name="tempXmlNode"></param>553
public void DeleteXmlNode(string tempXmlNode)554

{555
XmlNodeList nodePath = this.xmlDoc.SelectNodes(tempXmlNode);556
if (!(nodePath == null))557

{558
foreach (XmlNode xn in nodePath)559

{560
xn.ParentNode.RemoveChild(xn);561
}562
}563
}564
#endregion565

566

XML文档事件#region XML文档事件567

/**//// <summary>568
/// 节点插入事件569
/// </summary>570
/// <param name="src"></param>571
/// <param name="args"></param>572
private void nodeInsertEvent(Object src, XmlNodeChangedEventArgs args)573

{574
//保存设置575
SaveXmlDocument();576
}577

/**//// <summary>578
/// 节点删除事件579
/// </summary>580
/// <param name="src"></param>581
/// <param name="args"></param>582
private void nodeDeleteEvent(Object src, XmlNodeChangedEventArgs args)583

{584
//保存设置585
SaveXmlDocument();586
}587

/**//// <summary>588
/// 节点更新事件589
/// </summary>590
/// <param name="src"></param>591
/// <param name="args"></param>592
private void nodeUpdateEvent(Object src, XmlNodeChangedEventArgs args)593

{594
//保存设置595
SaveXmlDocument();596
}597
#endregion598

599

保存XML文件#region 保存XML文件600

/**//// <summary>601
/// 保存XML文件602
/// </summary>603
public void SaveXmlDocument()604

{605
try606

{607
if (this.xmlFilePathType == enumXmlPathType.AbsolutePath)608

{609
Savexml(xmlFilePath);610
}611
else612
if (this.xmlFilePathType == enumXmlPathType.VirtualPath)613

{614
Savexml(HttpContext.Current.Server.MapPath(xmlFilePath));615
}616
}617
catch (XmlException xmle)618

{619
throw xmle;620
}621
}622

/**//// <summary>623
/// 保存XML文件624
/// </summary>625
/// <param name="tempXMLFilePath"></param>626
public void SaveXmlDocument(string tempXMLFilePath)627

{628
try629

{630
//保存设置的结果631
Savexml(tempXMLFilePath);632
}633
catch (XmlException xmle)634

{635
throw xmle;636
}637
}638

/**//// <summary>639
/// 保存XML文件640
/// </summary>641
/// <param name="filepath"></param>642
private void Savexml(string filepath)643

{644
xmlDoc.Save(filepath);645
}646
#endregion647
}648
}649


浙公网安备 33010602011771号