900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > 详细介绍XML操作总结的示例代码

详细介绍XML操作总结的示例代码

时间:2024-01-27 17:27:11

相关推荐

详细介绍XML操作总结的示例代码

XML/RSS教程

XML操作

XML/RSS教程

在开发过程中对XML的使用不是太多,要用到时候也是想办法绕过去,最近一个同事给了一个详细的操作。分享一下

release的源码下载,vscode底栏代码,ubuntu 清理残留,tomcat检查是否启动,sqlite数据库有缓存吗,做梦梦见脚上爬虫子是什么意思,rss php 生成,快速seo优化费用,c语言代码网站,discuz 模板小说lzw

using System;using System.Collections.Generic;using System.Linq;using System.Web; using System.Xml; namespace RenShiExport{///public class XMLHelper { public XMLHelper() { // // TODO: 在此处添加构造函数逻辑 // }#region XML文档节点查询和读取 /// /// XML文档完全文件名(包含物理路径) /// 要匹配的XPath表达式(例如:"//节点名//子节点名") /// 返回XmlNode public static XmlNode GetXmlNodeByXpath(string xmlFileName, string xpath) { XmlDocument xmlDoc = new XmlDocument(); try {xmlDoc.Load(xmlFileName); //加载XML文档XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath);return xmlNode; } catch (Exception ex) {return null;//throw ex; //这里可以定义你自己的异常处理 } } /// /// XML文档完全文件名(包含物理路径) /// 要匹配的XPath表达式(例如:"//节点名//子节点名") /// 返回XmlNodeList public static XmlNodeList GetXmlNodeListByXpath(string xmlFileName, string xpath) { XmlDocument xmlDoc = new XmlDocument(); try {xmlDoc.Load(xmlFileName); //加载XML文档XmlNodeList xmlNodeList = xmlDoc.SelectNodes(xpath);return xmlNodeList; } catch (Exception ex) {return null;//throw ex; //这里可以定义你自己的异常处理 } } /// /// XML文档完全文件名(包含物理路径) /// 要匹配的XPath表达式(例如:"//节点名//子节点名 /// 要匹配xmlAttributeName的属性名称 /// 返回xmlAttributeName public static XmlAttribute GetXmlAttribute(string xmlFileName, string xpath, string xmlAttributeName) { string content = string.Empty; XmlDocument xmlDoc = new XmlDocument(); XmlAttribute xmlAttribute = null; try {xmlDoc.Load(xmlFileName); //加载XML文档XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath);if (xmlNode != null){ if (xmlNode.Attributes.Count > 0) { xmlAttribute = xmlNode.Attributes[xmlAttributeName]; }} } catch (Exception ex) {throw ex; //这里可以定义你自己的异常处理 } return xmlAttribute; } #endregion #region XML文档创建和节点或属性的添加、修改 /// /// XML文档完全文件名(包含物理路径) /// XML文档根节点名称(须指定一个根节点名称) /// XML文档版本号(必须为:"1.0") /// XML文档编码方式 /// 该值必须是"yes"或"no",如果为null,Save方法不在XML声明上写出独立属性 /// 成功返回true,失败返回false public static bool CreateXmlDocument(string xmlFileName, string rootNodeName, string version, string encoding, string standalone) { bool isSuccess = false; try {XmlDocument xmlDoc = new XmlDocument();XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration(version, encoding, standalone);XmlNode root = xmlDoc.CreateElement(rootNodeName);xmlDoc.AppendChild(xmlDeclaration);xmlDoc.AppendChild(root);xmlDoc.Save(xmlFileName);isSuccess = true; } catch (Exception ex) {throw ex; //这里可以定义你自己的异常处理 } return isSuccess; } /// /// XML文档完全文件名(包含物理路径) /// 要匹配的XPath表达式(例如:"//节点名//子节点名 /// 要匹配xmlNodeName的节点名称 /// 节点文本值 /// 要匹配xmlAttributeName的属性名称 /// 属性值 /// 成功返回true,失败返回false public static bool CreateXmlNodeByXPath(string xmlFileName, string xpath, string xmlNodeName, string innerText, string xmlAttributeName, string value) { bool isSuccess = false; XmlDocument xmlDoc = new XmlDocument(); try {xmlDoc.Load(xmlFileName); //加载XML文档XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath);if (xmlNode != null){ //存不存在此节点都创建 XmlElement subElement = xmlDoc.CreateElement(xmlNodeName); subElement.InnerXml = innerText;//如果属性和值参数都不为空则在此新节点上新增属性 if (!string.IsNullOrEmpty(xmlAttributeName) && !string.IsNullOrEmpty(value)) { XmlAttribute xmlAttribute = xmlDoc.CreateAttribute(xmlAttributeName); xmlAttribute.Value = value; subElement.Attributes.Append(xmlAttribute); }xmlNode.AppendChild(subElement);}xmlDoc.Save(xmlFileName); //保存到XML文档isSuccess = true; } catch (Exception ex) {throw ex; //这里可以定义你自己的异常处理 } return isSuccess; } /// /// XML文档完全文件名(包含物理路径) /// 要匹配的XPath表达式(例如:"//节点名//子节点名 /// 要匹配xmlNodeName的节点名称 /// 节点文本值 /// 成功返回true,失败返回false public static bool CreateOrUpdateXmlNodeByXPath(string xmlFileName, string xpath, string xmlNodeName, string innerText) { bool isSuccess = false; bool isExistsNode = false;//标识节点是否存在 XmlDocument xmlDoc = new XmlDocument(); try {xmlDoc.Load(xmlFileName); //加载XML文档XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath);if (xmlNode != null){ //遍历xpath节点下的所有子节点 foreach (XmlNode node in xmlNode.ChildNodes) { if (node.Name.ToLower() == xmlNodeName.ToLower()) { //存在此节点则更新 node.InnerXml = innerText; isExistsNode = true; break; } } if (!isExistsNode) { //不存在此节点则创建 XmlElement subElement = xmlDoc.CreateElement(xmlNodeName); subElement.InnerXml = innerText; xmlNode.AppendChild(subElement); }}xmlDoc.Save(xmlFileName); //保存到XML文档isSuccess = true; } catch (Exception ex) {throw ex; //这里可以定义你自己的异常处理 } return isSuccess; } /// /// XML文档完全文件名(包含物理路径) /// 要匹配的XPath表达式(例如:"//节点名//子节点名 /// 要匹配xmlAttributeName的属性名称 /// 属性值 /// 成功返回true,失败返回false public static bool CreateOrUpdateXmlAttributeByXPath(string xmlFileName, string xpath, string xmlAttributeName, string value) { bool isSuccess = false; bool isExistsAttribute = false;//标识属性是否存在 XmlDocument xmlDoc = new XmlDocument(); try {xmlDoc.Load(xmlFileName); //加载XML文档XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath);if (xmlNode != null){ //遍历xpath节点中的所有属性 foreach (XmlAttribute attribute in xmlNode.Attributes) { if (attribute.Name.ToLower() == xmlAttributeName.ToLower()) { //节点中存在此属性则更新 attribute.Value = value; isExistsAttribute = true; break; } } if (!isExistsAttribute) { //节点中不存在此属性则创建 XmlAttribute xmlAttribute = xmlDoc.CreateAttribute(xmlAttributeName); xmlAttribute.Value = value; xmlNode.Attributes.Append(xmlAttribute); }}xmlDoc.Save(xmlFileName); //保存到XML文档isSuccess = true; } catch (Exception ex) {throw ex; //这里可以定义你自己的异常处理 } return isSuccess; } #endregion#region XML文档节点或属性的删除 /// /// XML文档完全文件名(包含物理路径) /// 要匹配的XPath表达式(例如:"//节点名//子节点名 /// 成功返回true,失败返回false public static bool DeleteXmlNodeByXPath(string xmlFileName, string xpath) { bool isSuccess = false; XmlDocument xmlDoc = new XmlDocument(); try {xmlDoc.Load(xmlFileName); //加载XML文档XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath);if (xmlNode != null){ //删除节点 xmlNode.ParentNode.RemoveChild(xmlNode);}xmlDoc.Save(xmlFileName); //保存到XML文档isSuccess = true; } catch (Exception ex) {throw ex; //这里可以定义你自己的异常处理 } return isSuccess; } /// /// XML文档完全文件名(包含物理路径) /// 要匹配的XPath表达式(例如:"//节点名//子节点名 /// 要删除的xmlAttributeName的属性名称 /// 成功返回true,失败返回false public static bool DeleteXmlAttributeByXPath(string xmlFileName, string xpath, string xmlAttributeName) { bool isSuccess = false; bool isExistsAttribute = false; XmlDocument xmlDoc = new XmlDocument(); try {xmlDoc.Load(xmlFileName); //加载XML文档XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath);XmlAttribute xmlAttribute = null;if (xmlNode != null){ //遍历xpath节点中的所有属性 foreach (XmlAttribute attribute in xmlNode.Attributes) { if (attribute.Name.ToLower() == xmlAttributeName.ToLower()) { //节点中存在此属性 xmlAttribute = attribute; isExistsAttribute = true; break; } } if (isExistsAttribute) { //删除节点中的属性 xmlNode.Attributes.Remove(xmlAttribute); }}xmlDoc.Save(xmlFileName); //保存到XML文档isSuccess = true; } catch (Exception ex) {throw ex; //这里可以定义你自己的异常处理 } return isSuccess; } /// /// XML文档完全文件名(包含物理路径) /// 要匹配的XPath表达式(例如:"//节点名//子节点名 /// 成功返回true,失败返回false public static bool DeleteAllXmlAttributeByXPath(string xmlFileName, string xpath) { bool isSuccess = false; XmlDocument xmlDoc = new XmlDocument(); try {xmlDoc.Load(xmlFileName); //加载XML文档XmlNode xmlNode = xmlDoc.SelectSingleNode(xpath);if (xmlNode != null){ //遍历xpath节点中的所有属性 xmlNode.Attributes.RemoveAll();}xmlDoc.Save(xmlFileName); //保存到XML文档isSuccess = true; } catch (Exception ex) {throw ex; //这里可以定义你自己的异常处理 } return isSuccess; } #endregion}}

app 服务器端 源码,ubuntu的putty,tomcat7如何配置,爬虫证书登录,如何写php框架,罗源一般seo服务费lzw

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。