代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
CSharp
】
xml操作类
作者:
CSDN
/ 发布于
2012/1/11
/
816
<div><span style="color: rgb(0,0,255)">using</span><span style="color: rgb(0,0,0)"> System; </span><span style="color: rgb(0,0,255)">using</span><span style="color: rgb(0,0,0)"> System.Collections.Generic; </span><span style="color: rgb(0,0,255)">using</span><span style="color: rgb(0,0,0)"> System.Linq; </span><span style="color: rgb(0,0,255)">using</span><span style="color: rgb(0,0,0)"> System.Web; </span><span style="color: rgb(0,0,255)">using</span><span style="color: rgb(0,0,0)"> System.Xml; </span><span style="color: rgb(0,0,255)">namespace</span><span style="color: rgb(0,0,0)"> JqueryAjax.Code.Tools { </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><summary></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> XMLHelper XML文档操作管理器 </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"></summary></span><span style="color: rgb(128,128,128)"> </span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">public</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">class</span><span style="color: rgb(0,0,0)"> XMLHelper { </span><span style="color: rgb(0,0,255)">#region</span><span style="color: rgb(0,0,0)"> XML文档节点查询和读取</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><summary></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> 选择匹配XPath表达式的第一个节点XmlNode. </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"></summary></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><param name="xmlFileName"></span><span style="color: rgb(0,128,0)">XML文档完全文件名(包含物理路径)</span><span style="color: rgb(128,128,128)"></param></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><param name="xpath"></span><span style="color: rgb(0,128,0)">要匹配的XPath表达式(例如:"//节点名//子节点名")</span><span style="color: rgb(128,128,128)"></param></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><returns></span><span style="color: rgb(0,128,0)">返回XmlNode</span><span style="color: rgb(128,128,128)"></returns></span><span style="color: rgb(128,128,128)"> </span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">public</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">static</span><span style="color: rgb(0,0,0)"> XmlNode GetXmlNodeByXpath(</span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> xmlFileName, </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> xpath) { XmlDocument xmlDoc </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> XmlDocument(); </span><span style="color: rgb(0,0,255)">try</span><span style="color: rgb(0,0,0)"> { xmlDoc.Load(xmlFileName); </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">加载XML文档</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> XmlNode xmlNode </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> xmlDoc.SelectSingleNode(xpath); </span><span style="color: rgb(0,0,255)">return</span><span style="color: rgb(0,0,0)"> xmlNode; } </span><span style="color: rgb(0,0,255)">catch</span><span style="color: rgb(0,0,0)"> (Exception ex) { </span><span style="color: rgb(0,0,255)">return</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">null</span><span style="color: rgb(0,0,0)">; </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">throw ex; </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">这里可以定义你自己的异常处理</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> } } </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><summary></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> 选择匹配XPath表达式的节点列表XmlNodeList. </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"></summary></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><param name="xmlFileName"></span><span style="color: rgb(0,128,0)">XML文档完全文件名(包含物理路径)</span><span style="color: rgb(128,128,128)"></param></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><param name="xpath"></span><span style="color: rgb(0,128,0)">要匹配的XPath表达式(例如:"//节点名//子节点名")</span><span style="color: rgb(128,128,128)"></param></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><returns></span><span style="color: rgb(0,128,0)">返回XmlNodeList</span><span style="color: rgb(128,128,128)"></returns></span><span style="color: rgb(128,128,128)"> </span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">public</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">static</span><span style="color: rgb(0,0,0)"> XmlNodeList GetXmlNodeListByXpath(</span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> xmlFileName, </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> xpath) { XmlDocument xmlDoc </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> XmlDocument(); </span><span style="color: rgb(0,0,255)">try</span><span style="color: rgb(0,0,0)"> { xmlDoc.Load(xmlFileName); </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">加载XML文档</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> XmlNodeList xmlNodeList </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> xmlDoc.SelectNodes(xpath); </span><span style="color: rgb(0,0,255)">return</span><span style="color: rgb(0,0,0)"> xmlNodeList; } </span><span style="color: rgb(0,0,255)">catch</span><span style="color: rgb(0,0,0)"> (Exception ex) { </span><span style="color: rgb(0,0,255)">return</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">null</span><span style="color: rgb(0,0,0)">; </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">throw ex; </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">这里可以定义你自己的异常处理</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> } } </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><summary></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> 选择匹配XPath表达式的第一个节点的匹配xmlAttributeName的属性XmlAttribute. </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"></summary></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><param name="xmlFileName"></span><span style="color: rgb(0,128,0)">XML文档完全文件名(包含物理路径)</span><span style="color: rgb(128,128,128)"></param></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><param name="xpath"></span><span style="color: rgb(0,128,0)">要匹配的XPath表达式(例如:"//节点名//子节点名</span><span style="color: rgb(128,128,128)"></param></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><param name="xmlAttributeName"></span><span style="color: rgb(0,128,0)">要匹配xmlAttributeName的属性名称</span><span style="color: rgb(128,128,128)"></param></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><returns></span><span style="color: rgb(0,128,0)">返回xmlAttributeName</span><span style="color: rgb(128,128,128)"></returns></span><span style="color: rgb(128,128,128)"> </span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">public</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">static</span><span style="color: rgb(0,0,0)"> XmlAttribute GetXmlAttribute(</span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> xmlFileName, </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> xpath, </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> xmlAttributeName) { </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> content </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)">.Empty; XmlDocument xmlDoc </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> XmlDocument(); XmlAttribute xmlAttribute </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">null</span><span style="color: rgb(0,0,0)">; </span><span style="color: rgb(0,0,255)">try</span><span style="color: rgb(0,0,0)"> { xmlDoc.Load(xmlFileName); </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">加载XML文档</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> XmlNode xmlNode </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> xmlDoc.SelectSingleNode(xpath); </span><span style="color: rgb(0,0,255)">if</span><span style="color: rgb(0,0,0)"> (xmlNode </span><span style="color: rgb(0,0,0)">!=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">null</span><span style="color: rgb(0,0,0)">) { </span><span style="color: rgb(0,0,255)">if</span><span style="color: rgb(0,0,0)"> (xmlNode.Attributes.Count </span><span style="color: rgb(0,0,0)">></span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,0,128)">0</span><span style="color: rgb(0,0,0)">) { xmlAttribute </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> xmlNode.Attributes[xmlAttributeName]; } } } </span><span style="color: rgb(0,0,255)">catch</span><span style="color: rgb(0,0,0)"> (Exception ex) { </span><span style="color: rgb(0,0,255)">throw</span><span style="color: rgb(0,0,0)"> ex; </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">这里可以定义你自己的异常处理</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> } </span><span style="color: rgb(0,0,255)">return</span><span style="color: rgb(0,0,0)"> xmlAttribute; } </span><span style="color: rgb(0,0,255)">#endregion</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">#region</span><span style="color: rgb(0,0,0)"> XML文档创建和节点或属性的添加、修改</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><summary></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> 创建一个XML文档 </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"></summary></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><param name="xmlFileName"></span><span style="color: rgb(0,128,0)">XML文档完全文件名(包含物理路径)</span><span style="color: rgb(128,128,128)"></param></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><param name="rootNodeName"></span><span style="color: rgb(0,128,0)">XML文档根节点名称(须指定一个根节点名称)</span><span style="color: rgb(128,128,128)"></param></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><param name="version"></span><span style="color: rgb(0,128,0)">XML文档版本号(必须为:"1.0")</span><span style="color: rgb(128,128,128)"></param></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><param name="encoding"></span><span style="color: rgb(0,128,0)">XML文档编码方式</span><span style="color: rgb(128,128,128)"></param></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><param name="standalone"></span><span style="color: rgb(0,128,0)">该值必须是"yes"或"no",如果为null,Save方法不在XML声明上写出独立属性</span><span style="color: rgb(128,128,128)"></param></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><returns></span><span style="color: rgb(0,128,0)">成功返回true,失败返回false</span><span style="color: rgb(128,128,128)"></returns></span><span style="color: rgb(128,128,128)"> </span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">public</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">static</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">bool</span><span style="color: rgb(0,0,0)"> CreateXmlDocument(</span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> xmlFileName, </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> rootNodeName, </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> version, </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> encoding, </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> standalone) { </span><span style="color: rgb(0,0,255)">bool</span><span style="color: rgb(0,0,0)"> isSuccess </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">false</span><span style="color: rgb(0,0,0)">; </span><span style="color: rgb(0,0,255)">try</span><span style="color: rgb(0,0,0)"> { XmlDocument xmlDoc </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> XmlDocument(); XmlDeclaration xmlDeclaration </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> xmlDoc.CreateXmlDeclaration(version, encoding, standalone); XmlNode root </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> xmlDoc.CreateElement(rootNodeName); xmlDoc.AppendChild(xmlDeclaration); xmlDoc.AppendChild(root); xmlDoc.Save(xmlFileName); isSuccess </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">true</span><span style="color: rgb(0,0,0)">; } </span><span style="color: rgb(0,0,255)">catch</span><span style="color: rgb(0,0,0)"> (Exception ex) { </span><span style="color: rgb(0,0,255)">throw</span><span style="color: rgb(0,0,0)"> ex; </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">这里可以定义你自己的异常处理</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> } </span><span style="color: rgb(0,0,255)">return</span><span style="color: rgb(0,0,0)"> isSuccess; } </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><summary></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> 依据匹配XPath表达式的第一个节点来创建它的子节点(如果此节点已存在则追加一个新的同名节点 </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"></summary></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><param name="xmlFileName"></span><span style="color: rgb(0,128,0)">XML文档完全文件名(包含物理路径)</span><span style="color: rgb(128,128,128)"></param></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><param name="xpath"></span><span style="color: rgb(0,128,0)">要匹配的XPath表达式(例如:"//节点名//子节点名</span><span style="color: rgb(128,128,128)"></param></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><param name="xmlNodeName"></span><span style="color: rgb(0,128,0)">要匹配xmlNodeName的节点名称</span><span style="color: rgb(128,128,128)"></param></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><param name="innerText"></span><span style="color: rgb(0,128,0)">节点文本值</span><span style="color: rgb(128,128,128)"></param></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><param name="xmlAttributeName"></span><span style="color: rgb(0,128,0)">要匹配xmlAttributeName的属性名称</span><span style="color: rgb(128,128,128)"></param></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><param name="value"></span><span style="color: rgb(0,128,0)">属性值</span><span style="color: rgb(128,128,128)"></param></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><returns></span><span style="color: rgb(0,128,0)">成功返回true,失败返回false</span><span style="color: rgb(128,128,128)"></returns></span><span style="color: rgb(128,128,128)"> </span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">public</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">static</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">bool</span><span style="color: rgb(0,0,0)"> CreateXmlNodeByXPath(</span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> xmlFileName, </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> xpath, </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> xmlNodeName, </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> innerText, </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> xmlAttributeName, </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> value) { </span><span style="color: rgb(0,0,255)">bool</span><span style="color: rgb(0,0,0)"> isSuccess </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">false</span><span style="color: rgb(0,0,0)">; XmlDocument xmlDoc </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> XmlDocument(); </span><span style="color: rgb(0,0,255)">try</span><span style="color: rgb(0,0,0)"> { xmlDoc.Load(xmlFileName); </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">加载XML文档</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> XmlNode xmlNode </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> xmlDoc.SelectSingleNode(xpath); </span><span style="color: rgb(0,0,255)">if</span><span style="color: rgb(0,0,0)"> (xmlNode </span><span style="color: rgb(0,0,0)">!=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">null</span><span style="color: rgb(0,0,0)">) { </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">存不存在此节点都创建</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> XmlElement subElement </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> xmlDoc.CreateElement(xmlNodeName); subElement.InnerXml </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> innerText; </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">如果属性和值参数都不为空则在此新节点上新增属性</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">if</span><span style="color: rgb(0,0,0)"> (</span><span style="color: rgb(0,0,0)">!</span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)">.IsNullOrEmpty(xmlAttributeName) </span><span style="color: rgb(0,0,0)">&&</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,0)">!</span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)">.IsNullOrEmpty(value)) { XmlAttribute xmlAttribute </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> xmlDoc.CreateAttribute(xmlAttributeName); xmlAttribute.Value </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> value; subElement.Attributes.Append(xmlAttribute); } xmlNode.AppendChild(subElement); } xmlDoc.Save(xmlFileName); </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">保存到XML文档</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> isSuccess </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">true</span><span style="color: rgb(0,0,0)">; } </span><span style="color: rgb(0,0,255)">catch</span><span style="color: rgb(0,0,0)"> (Exception ex) { </span><span style="color: rgb(0,0,255)">throw</span><span style="color: rgb(0,0,0)"> ex; </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">这里可以定义你自己的异常处理</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> } </span><span style="color: rgb(0,0,255)">return</span><span style="color: rgb(0,0,0)"> isSuccess; } </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><summary></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> 依据匹配XPath表达式的第一个节点来创建或更新它的子节点(如果节点存在则更新,不存在则创建) </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"></summary></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><param name="xmlFileName"></span><span style="color: rgb(0,128,0)">XML文档完全文件名(包含物理路径)</span><span style="color: rgb(128,128,128)"></param></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><param name="xpath"></span><span style="color: rgb(0,128,0)">要匹配的XPath表达式(例如:"//节点名//子节点名</span><span style="color: rgb(128,128,128)"></param></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><param name="xmlNodeName"></span><span style="color: rgb(0,128,0)">要匹配xmlNodeName的节点名称</span><span style="color: rgb(128,128,128)"></param></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><param name="innerText"></span><span style="color: rgb(0,128,0)">节点文本值</span><span style="color: rgb(128,128,128)"></param></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><returns></span><span style="color: rgb(0,128,0)">成功返回true,失败返回false</span><span style="color: rgb(128,128,128)"></returns></span><span style="color: rgb(128,128,128)"> </span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">public</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">static</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">bool</span><span style="color: rgb(0,0,0)"> CreateOrUpdateXmlNodeByXPath(</span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> xmlFileName, </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> xpath, </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> xmlNodeName, </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> innerText) { </span><span style="color: rgb(0,0,255)">bool</span><span style="color: rgb(0,0,0)"> isSuccess </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">false</span><span style="color: rgb(0,0,0)">; </span><span style="color: rgb(0,0,255)">bool</span><span style="color: rgb(0,0,0)"> isExistsNode </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">false</span><span style="color: rgb(0,0,0)">;</span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">标识节点是否存在</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> XmlDocument xmlDoc </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> XmlDocument(); </span><span style="color: rgb(0,0,255)">try</span><span style="color: rgb(0,0,0)"> { xmlDoc.Load(xmlFileName); </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">加载XML文档</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> XmlNode xmlNode </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> xmlDoc.SelectSingleNode(xpath); </span><span style="color: rgb(0,0,255)">if</span><span style="color: rgb(0,0,0)"> (xmlNode </span><span style="color: rgb(0,0,0)">!=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">null</span><span style="color: rgb(0,0,0)">) { </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">遍历xpath节点下的所有子节点</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">foreach</span><span style="color: rgb(0,0,0)"> (XmlNode node </span><span style="color: rgb(0,0,255)">in</span><span style="color: rgb(0,0,0)"> xmlNode.ChildNodes) { </span><span style="color: rgb(0,0,255)">if</span><span style="color: rgb(0,0,0)"> (node.Name.ToLower() </span><span style="color: rgb(0,0,0)">==</span><span style="color: rgb(0,0,0)"> xmlNodeName.ToLower()) { </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">存在此节点则更新</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> node.InnerXml </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> innerText; isExistsNode </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">true</span><span style="color: rgb(0,0,0)">; </span><span style="color: rgb(0,0,255)">break</span><span style="color: rgb(0,0,0)">; } } </span><span style="color: rgb(0,0,255)">if</span><span style="color: rgb(0,0,0)"> (</span><span style="color: rgb(0,0,0)">!</span><span style="color: rgb(0,0,0)">isExistsNode) { </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">不存在此节点则创建</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> XmlElement subElement </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> xmlDoc.CreateElement(xmlNodeName); subElement.InnerXml </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> innerText; xmlNode.AppendChild(subElement); } } xmlDoc.Save(xmlFileName); </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">保存到XML文档</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> isSuccess </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">true</span><span style="color: rgb(0,0,0)">; } </span><span style="color: rgb(0,0,255)">catch</span><span style="color: rgb(0,0,0)"> (Exception ex) { </span><span style="color: rgb(0,0,255)">throw</span><span style="color: rgb(0,0,0)"> ex; </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">这里可以定义你自己的异常处理</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> } </span><span style="color: rgb(0,0,255)">return</span><span style="color: rgb(0,0,0)"> isSuccess; } </span> <span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><summary></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> 依据匹配XPath表达式的第一个节点来创建或更新它的属性(如果属性存在则更新,不存在则创建) </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"></summary></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><param name="xmlFileName"></span><span style="color: rgb(0,128,0)">XML文档完全文件名(包含物理路径)</span><span style="color: rgb(128,128,128)"></param></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><param name="xpath"></span><span style="color: rgb(0,128,0)">要匹配的XPath表达式(例如:"//节点名//子节点名</span><span style="color: rgb(128,128,128)"></param></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><param name="xmlAttributeName"></span><span style="color: rgb(0,128,0)">要匹配xmlAttributeName的属性名称</span><span style="color: rgb(128,128,128)"></param></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><param name="value"></span><span style="color: rgb(0,128,0)">属性值</span><span style="color: rgb(128,128,128)"></param></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><returns></span><span style="color: rgb(0,128,0)">成功返回true,失败返回false</span><span style="color: rgb(128,128,128)"></returns></span><span style="color: rgb(128,128,128)"> </span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">public</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">static</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">bool</span><span style="color: rgb(0,0,0)"> CreateOrUpdateXmlAttributeByXPath(</span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> xmlFileName, </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> xpath, </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> xmlAttributeName, </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> value) { </span><span style="color: rgb(0,0,255)">bool</span><span style="color: rgb(0,0,0)"> isSuccess </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">false</span><span style="color: rgb(0,0,0)">; </span><span style="color: rgb(0,0,255)">bool</span><span style="color: rgb(0,0,0)"> isExistsAttribute </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">false</span><span style="color: rgb(0,0,0)">;</span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">标识属性是否存在</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> XmlDocument xmlDoc </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> XmlDocument(); </span><span style="color: rgb(0,0,255)">try</span><span style="color: rgb(0,0,0)"> { xmlDoc.Load(xmlFileName); </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">加载XML文档</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> XmlNode xmlNode </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> xmlDoc.SelectSingleNode(xpath); </span><span style="color: rgb(0,0,255)">if</span><span style="color: rgb(0,0,0)"> (xmlNode </span><span style="color: rgb(0,0,0)">!=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">null</span><span style="color: rgb(0,0,0)">) { </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">遍历xpath节点中的所有属性</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">foreach</span><span style="color: rgb(0,0,0)"> (XmlAttribute attribute </span><span style="color: rgb(0,0,255)">in</span><span style="color: rgb(0,0,0)"> xmlNode.Attributes) { </span><span style="color: rgb(0,0,255)">if</span><span style="color: rgb(0,0,0)"> (attribute.Name.ToLower() </span><span style="color: rgb(0,0,0)">==</span><span style="color: rgb(0,0,0)"> xmlAttributeName.ToLower()) { </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">节点中存在此属性则更新</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> attribute.Value </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> value; isExistsAttribute </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">true</span><span style="color: rgb(0,0,0)">; </span><span style="color: rgb(0,0,255)">break</span><span style="color: rgb(0,0,0)">; } } </span><span style="color: rgb(0,0,255)">if</span><span style="color: rgb(0,0,0)"> (</span><span style="color: rgb(0,0,0)">!</span><span style="color: rgb(0,0,0)">isExistsAttribute) { </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">节点中不存在此属性则创建</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> XmlAttribute xmlAttribute </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> xmlDoc.CreateAttribute(xmlAttributeName); xmlAttribute.Value </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> value; xmlNode.Attributes.Append(xmlAttribute); } } xmlDoc.Save(xmlFileName); </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">保存到XML文档</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> isSuccess </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">true</span><span style="color: rgb(0,0,0)">; } </span><span style="color: rgb(0,0,255)">catch</span><span style="color: rgb(0,0,0)"> (Exception ex) { </span><span style="color: rgb(0,0,255)">throw</span><span style="color: rgb(0,0,0)"> ex; </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">这里可以定义你自己的异常处理</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> } </span><span style="color: rgb(0,0,255)">return</span><span style="color: rgb(0,0,0)"> isSuccess; } </span><span style="color: rgb(0,0,255)">#endregion</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">#region</span><span style="color: rgb(0,0,0)"> XML文档节点或属性的删除</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><summary></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> 删除匹配XPath表达式的第一个节点(节点中的子元素同时会被删除) </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"></summary></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><param name="xmlFileName"></span><span style="color: rgb(0,128,0)">XML文档完全文件名(包含物理路径)</span><span style="color: rgb(128,128,128)"></param></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><param name="xpath"></span><span style="color: rgb(0,128,0)">要匹配的XPath表达式(例如:"//节点名//子节点名</span><span style="color: rgb(128,128,128)"></param></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><returns></span><span style="color: rgb(0,128,0)">成功返回true,失败返回false</span><span style="color: rgb(128,128,128)"></returns></span><span style="color: rgb(128,128,128)"> </span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">public</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">static</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">bool</span><span style="color: rgb(0,0,0)"> DeleteXmlNodeByXPath(</span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> xmlFileName, </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> xpath) { </span><span style="color: rgb(0,0,255)">bool</span><span style="color: rgb(0,0,0)"> isSuccess </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">false</span><span style="color: rgb(0,0,0)">; XmlDocument xmlDoc </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> XmlDocument(); </span><span style="color: rgb(0,0,255)">try</span><span style="color: rgb(0,0,0)"> { xmlDoc.Load(xmlFileName); </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">加载XML文档</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> XmlNode xmlNode </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> xmlDoc.SelectSingleNode(xpath); </span><span style="color: rgb(0,0,255)">if</span><span style="color: rgb(0,0,0)"> (xmlNode </span><span style="color: rgb(0,0,0)">!=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">null</span><span style="color: rgb(0,0,0)">) { </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">删除节点</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> xmlNode.ParentNode.RemoveChild(xmlNode); } xmlDoc.Save(xmlFileName); </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">保存到XML文档</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> isSuccess </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">true</span><span style="color: rgb(0,0,0)">; } </span><span style="color: rgb(0,0,255)">catch</span><span style="color: rgb(0,0,0)"> (Exception ex) { </span><span style="color: rgb(0,0,255)">throw</span><span style="color: rgb(0,0,0)"> ex; </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">这里可以定义你自己的异常处理</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> } </span><span style="color: rgb(0,0,255)">return</span><span style="color: rgb(0,0,0)"> isSuccess; } </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><summary></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> 删除匹配XPath表达式的第一个节点中的匹配参数xmlAttributeName的属性 </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"></summary></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><param name="xmlFileName"></span><span style="color: rgb(0,128,0)">XML文档完全文件名(包含物理路径)</span><span style="color: rgb(128,128,128)"></param></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><param name="xpath"></span><span style="color: rgb(0,128,0)">要匹配的XPath表达式(例如:"//节点名//子节点名</span><span style="color: rgb(128,128,128)"></param></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><param name="xmlAttributeName"></span><span style="color: rgb(0,128,0)">要删除的xmlAttributeName的属性名称</span><span style="color: rgb(128,128,128)"></param></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><returns></span><span style="color: rgb(0,128,0)">成功返回true,失败返回false</span><span style="color: rgb(128,128,128)"></returns></span><span style="color: rgb(128,128,128)"> </span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">public</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">static</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">bool</span><span style="color: rgb(0,0,0)"> DeleteXmlAttributeByXPath(</span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> xmlFileName, </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> xpath, </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> xmlAttributeName) { </span><span style="color: rgb(0,0,255)">bool</span><span style="color: rgb(0,0,0)"> isSuccess </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">false</span><span style="color: rgb(0,0,0)">; </span><span style="color: rgb(0,0,255)">bool</span><span style="color: rgb(0,0,0)"> isExistsAttribute </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">false</span><span style="color: rgb(0,0,0)">; XmlDocument xmlDoc </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> XmlDocument(); </span><span style="color: rgb(0,0,255)">try</span><span style="color: rgb(0,0,0)"> { xmlDoc.Load(xmlFileName); </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">加载XML文档</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> XmlNode xmlNode </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> xmlDoc.SelectSingleNode(xpath); XmlAttribute xmlAttribute </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">null</span><span style="color: rgb(0,0,0)">; </span><span style="color: rgb(0,0,255)">if</span><span style="color: rgb(0,0,0)"> (xmlNode </span><span style="color: rgb(0,0,0)">!=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">null</span><span style="color: rgb(0,0,0)">) { </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">遍历xpath节点中的所有属性</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">foreach</span><span style="color: rgb(0,0,0)"> (XmlAttribute attribute </span><span style="color: rgb(0,0,255)">in</span><span style="color: rgb(0,0,0)"> xmlNode.Attributes) { </span><span style="color: rgb(0,0,255)">if</span><span style="color: rgb(0,0,0)"> (attribute.Name.ToLower() </span><span style="color: rgb(0,0,0)">==</span><span style="color: rgb(0,0,0)"> xmlAttributeName.ToLower()) { </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">节点中存在此属性</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> xmlAttribute </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> attribute; isExistsAttribute </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">true</span><span style="color: rgb(0,0,0)">; </span><span style="color: rgb(0,0,255)">break</span><span style="color: rgb(0,0,0)">; } } </span><span style="color: rgb(0,0,255)">if</span><span style="color: rgb(0,0,0)"> (isExistsAttribute) { </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">删除节点中的属性</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> xmlNode.Attributes.Remove(xmlAttribute); } } xmlDoc.Save(xmlFileName); </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">保存到XML文档</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> isSuccess </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">true</span><span style="color: rgb(0,0,0)">; } </span><span style="color: rgb(0,0,255)">catch</span><span style="color: rgb(0,0,0)"> (Exception ex) { </span><span style="color: rgb(0,0,255)">throw</span><span style="color: rgb(0,0,0)"> ex; </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">这里可以定义你自己的异常处理</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> } </span><span style="color: rgb(0,0,255)">return</span><span style="color: rgb(0,0,0)"> isSuccess; } </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><summary></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> 删除匹配XPath表达式的第一个节点中的所有属性 </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"></summary></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><param name="xmlFileName"></span><span style="color: rgb(0,128,0)">XML文档完全文件名(包含物理路径)</span><span style="color: rgb(128,128,128)"></param></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><param name="xpath"></span><span style="color: rgb(0,128,0)">要匹配的XPath表达式(例如:"//节点名//子节点名</span><span style="color: rgb(128,128,128)"></param></span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)">///</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(128,128,128)"><returns></span><span style="color: rgb(0,128,0)">成功返回true,失败返回false</span><span style="color: rgb(128,128,128)"></returns></span><span style="color: rgb(128,128,128)"> </span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">public</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">static</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">bool</span><span style="color: rgb(0,0,0)"> DeleteAllXmlAttributeByXPath(</span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> xmlFileName, </span><span style="color: rgb(0,0,255)">string</span><span style="color: rgb(0,0,0)"> xpath) { </span><span style="color: rgb(0,0,255)">bool</span><span style="color: rgb(0,0,0)"> isSuccess </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">false</span><span style="color: rgb(0,0,0)">; XmlDocument xmlDoc </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">new</span><span style="color: rgb(0,0,0)"> XmlDocument(); </span><span style="color: rgb(0,0,255)">try</span><span style="color: rgb(0,0,0)"> { xmlDoc.Load(xmlFileName); </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">加载XML文档</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> XmlNode xmlNode </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> xmlDoc.SelectSingleNode(xpath); </span><span style="color: rgb(0,0,255)">if</span><span style="color: rgb(0,0,0)"> (xmlNode </span><span style="color: rgb(0,0,0)">!=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">null</span><span style="color: rgb(0,0,0)">) { </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">遍历xpath节点中的所有属性</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> xmlNode.Attributes.RemoveAll(); } xmlDoc.Save(xmlFileName); </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">保存到XML文档</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> isSuccess </span><span style="color: rgb(0,0,0)">=</span><span style="color: rgb(0,0,0)"> </span><span style="color: rgb(0,0,255)">true</span><span style="color: rgb(0,0,0)">; } </span><span style="color: rgb(0,0,255)">catch</span><span style="color: rgb(0,0,0)"> (Exception ex) { </span><span style="color: rgb(0,0,255)">throw</span><span style="color: rgb(0,0,0)"> ex; </span><span style="color: rgb(0,128,0)">//</span><span style="color: rgb(0,128,0)">这里可以定义你自己的异常处理</span><span style="color: rgb(0,128,0)"> </span><span style="color: rgb(0,0,0)"> } </span><span style="color: rgb(0,0,255)">return</span><span style="color: rgb(0,0,0)"> isSuccess; } </span><span style="color: rgb(0,0,255)">#endregion</span><span style="color: rgb(0,0,0)"> } } </span></div>
试试其它关键字
xml操作类
同语言下
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
CSDN
贡献的其它代码
(
27
)
.
xml操作类
.
完全无组件,不用调用Excel.exe 导出数据到Excel
.
DropDownList手动构造一个下拉树形
.
判断是否低于IE6的版本
.
javascript 鼠标拖拉画图
.
取汉字首字母
.
字符串格式化通用类
.
mssql海量高效分页存储过程
.
.net 获取远程IP 得到省市
.
javascript 模仿windows拖动 封装类
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3