XML操作:1.XML类(http://blog.csdn.net/happy09li/article/details/7460521)
- XML绑定TreeView
- private void XmlOperation_Load(object sender, EventArgs e)
- {
- path = AppDomain.CurrentDomain.BaseDirectory + @"NameList.xml";
- xml.Load(path);//加载xml文件
- bindTvXml();
- }
- /// <summary>
- /// 绑定TreeView
- /// </summary>
- private void bindTvXml()
- {
- for (int i = 0; i < xml.DocumentElement.ChildNodes.Count; i++)
- {
- XmlNode Xnode = xml.DocumentElement.ChildNodes[i];
- TreeNode node = new TreeNode();
- node.Text = Xnode.Attributes["name"].Value;
- node.Tag = Xnode;
- bindChildNode(node, Xnode);//绑定子节点
- TvXml.Nodes.Add(node);
- TvXml.HideSelection = false;
- }
- }
- /// <summary>
- /// 递归绑定子节点
- /// </summary>
- /// <param name="node"></param>
- /// <param name="xml"></param>
- private void bindChildNode(TreeNode node, XmlNode xml)
- {
- for (int i = 0; i < xml.ChildNodes.Count; i++)
- {
- TreeNode Childnode = new TreeNode();
- XmlNode ChildXml = xml.ChildNodes[i];
- Childnode.Text = ChildXml.Value;
- Childnode.Name = "1";
- Childnode.Tag = xml.ChildNodes[i];
- if (ChildXml.HasChildNodes)
- {
- if (ChildXml.ChildNodes[0].NodeType == XmlNodeType.Text)
- Childnode.Text = ChildXml.ChildNodes[0].InnerText;
- else
- bindChildNode(Childnode, ChildXml);
- }
- node.Nodes.Add(Childnode);
- }
- }
- public class ManageXML
- {
- /// <summary>
- /// 返回用户操作的SQL语句
- /// </summary>
- /// <param name="sqlNodeName">XML的操作节点</param>
- /// <returns>操作的sql语句</returns>
- public static string GetXMLPath(string strXMlFileName)
- {
- string m_strFullPath = "";
- Assembly Asm = Assembly.GetExecutingAssembly();
- //获取文件的路径
- //m_strFullPath = Asm.Location.Substring(0, (Asm.Location.LastIndexOf("\\") + 1)) + "XMLSql.xml";
- m_strFullPath = AppDomain.CurrentDomain.BaseDirectory + "XMLLibrary\\" + strXMlFileName;
- return m_strFullPath;
- }
- /// <summary>
- /// 获取XML数据库中的数据的方法
- /// </summary>
- /// <param name="strFilePath">传入文件路径</param>
- /// <returns>返回一个数据集</returns>
- public static DataSet GetAllDataFromXML(string strFilePath)
- {
- DataSet ds = new DataSet();
- FileInfo fileInfo = new FileInfo(strFilePath);
- if (fileInfo.Exists)
- {
- try
- {
- ds.ReadXml(strFilePath);
- }
- catch { }
- }
- else
- {
- ds = null;
- }
- if (ds != null)
- {
- if (ds.Tables[0].Rows.Count < 1)
- ds = null;
- }
- return ds;
- }
- /// <summary>
- /// 获取指定目录下所有子节点的值
- /// </summary>
- /// <param name="strFileName">文件路径</param>
- /// <param name="nodeDir">节点目录</param>
- /// <returns></returns>
- public static Hashtable GetNodeList(string strFileName, string nodeDir)
- {
- Hashtable strNodeList = new Hashtable();
- try
- {
- XmlDocument xmlDoc = new XmlDocument();
- xmlDoc.Load(strFileName);
- XmlNodeList nodeList = xmlDoc.SelectSingleNode(nodeDir).ChildNodes;//获取bookstore节点的所有子节点
- foreach (XmlNode xn in nodeList) //遍历所有子节点
- {
- XmlElement xe = (XmlElement)xn; //将子节点类型转换为XmlElement类型
- strNodeList.Add(xe.GetAttribute("id").ToString(), xe.InnerText.Trim());
- }
- }
- catch (Exception)
- {
- throw;
- }
- return strNodeList;
- }
- /// <summary>
- /// 获取指定节点的值
- /// </summary>
- /// <param name="strFileName">文件路径</param>
- /// <param name="nodeName">节点名称</param>
- /// <param name="value">设置后的值</param>
- /// <param name="nodeDir">指定节点所在的节点目录</param>
- /// <returns></returns>
- public static string GetNodeValue(string strFileName, string nodeName, string nodeDir)
- {
- string value = null;
- try
- {
- XmlDocument xmlDoc = new XmlDocument();
- xmlDoc.Load(strFileName);
- XmlNodeList nodeList = xmlDoc.SelectSingleNode(nodeDir).ChildNodes;//获取bookstore节点的所有子节点
- foreach (XmlNode xn in nodeList) //遍历所有子节点
- {
- XmlElement xe = (XmlElement)xn; //将子节点类型转换为XmlElement类型
- if (xe.Name == nodeName)
- {
- value = xe.InnerText.Trim();
- break;
- }
- }
- }
- catch (Exception exp)
- {
- throw exp;
- }
- return value;
- }
- /// <summary>
- /// 获取指定节点下面对应属性的值
- /// </summary>
- /// <param name="strFileName">文件路径</param>
- /// <param name="nodeName">节点名称</param>
- /// <param name="nodeDir">指定节点所在的节点目录</param>
- /// <param name="attribute">节点对应的属性名称</param>
- /// <returns></returns>
- public static string GetNodeValue(string strFileName, string nodeName, string nodeDir, string attribute)
- {
- string value = null;
- try
- {
- XmlDocument xmlDoc = new XmlDocument();
- xmlDoc.Load(strFileName);
- XmlNodeList nodeList = xmlDoc.SelectSingleNode(nodeDir).ChildNodes;//获取bookstore节点的所有子节点
- foreach (XmlNode xn in nodeList) //遍历所有子节点
- {
- XmlElement xe = (XmlElement)xn; //将子节点类型转换为XmlElement类型
- if (xe.Name == nodeName)
- {
- //value = xe.InnerText.Trim();
- value = (xe).Attributes[attribute].Value;
- break;
- }
- }
- }
- catch (Exception exp)
- {
- throw exp;
- }
- return value;
- }
- /// <summary>
- /// 修改指定结点值
- /// </summary>
- /// <param name="strFileName">文件路径</param>
- /// <param name="nodeName">节点名称</param>
- /// <param name="value">设置后的值</param>
- /// <param name="nodeDir">指定节点所在的节点目录</param>
- /// <returns></returns>
- public static bool UpdateNoteValue(string strFileName, string nodeName, string value, string nodeDir)
- {
- bool isSucceed = false;
- try
- {
- XmlDocument xmlDoc = new XmlDocument();
- xmlDoc.Load(strFileName);
- XmlNodeList nodeList = xmlDoc.SelectSingleNode(nodeDir).ChildNodes;//获取bookstore节点的所有子节点
- foreach (XmlNode xn in nodeList) //遍历所有子节点
- {
- XmlElement xe = (XmlElement)xn; //将子节点类型转换为XmlElement类型
- if (xe.Name == nodeName)
- {
- xe.InnerText = value;
- isSucceed = true;
- break;
- }
- }
- xmlDoc.Save(strFileName);
- }
- catch (Exception exp)
- {
- throw exp;
- }
- return isSucceed;
- }
- /// <summary>
- /// 修改指定结点值
- /// </summary>
- /// <param name="strFileName">文件路径</param>
- /// <param name="nodeName">节点名称</param>
- /// <param name="value">设置后的值</param>
- /// <param name="nodeDir">指定节点所在的节点目录</param>
- /// <returns></returns>
- public static bool UpdateNoteValue(string strFileName, string nodeName, string value, string nodeDir, string attribute, string attributeValue)
- {
- bool isSucceed = false;
- try
- {
- XmlDocument xmlDoc = new XmlDocument();
- xmlDoc.Load(strFileName);
- XmlNodeList nodeList = xmlDoc.SelectSingleNode(nodeDir).ChildNodes;//获取bookstore节点的所有子节点
- foreach (XmlNode xn in nodeList) //遍历所有子节点
- {
- XmlElement xe = (XmlElement)xn; //将子节点类型转换为XmlElement类型
- if (xe.Name == nodeName)
- {
- xe.InnerText = value;
- (xe).Attributes[attribute].Value = attributeValue;
- isSucceed = true;
- break;
- }
- }
- xmlDoc.Save(strFileName);
- }
- catch (Exception exp)
- {
- throw exp;
- }
- return isSucceed;
- }
- /// <summary>
- /// 修改指定结点值
- /// </summary>
- /// <param name="strFileName">文件路径</param>
- /// <param name="nodeName">节点名称</param>
- /// <param name="value">设置后的值</param>
- /// <param name="nodeDir">指定节点所在的节点目录</param>
- /// <returns></returns>
- public static bool UpdateNoteValue(string strFileName, Hashtable hstable, string nodeDir)
- {
- bool isSucceed = false;
- try
- {
- XmlDocument xmlDoc = new XmlDocument();
- xmlDoc.Load(strFileName);
- XmlNodeList nodeList = xmlDoc.SelectSingleNode(nodeDir).ChildNodes;//获取bookstore节点的所有子节点
- foreach (DictionaryEntry item in hstable)
- {
- foreach (XmlNode xn in nodeList) //遍历所有子节点
- {
- XmlElement xe = (XmlElement)xn; //将子节点类型转换为XmlElement类型
- if (xe.Name == item.Key.ToString())
- {
- xe.InnerText = item.Value.ToString();
- isSucceed = true;
- break;
- }
- }
- }
- xmlDoc.Save(strFileName);
- }
- catch (Exception exp)
- {
- throw exp;
- }
- return isSucceed;
- }
- }
- /// <summary>
- /// 移除选定节点集的所有属性
- /// </summary>
- /// <param name="xmlPathNode"></param>
- public void RemoveAllAttribute(string xmlPathNode)
- {
- XmlNodeList xnl = objXmlDoc.SelectNodes(xmlPathNode);
- foreach (XmlNode xn in xnl)
- {
- xn.Attributes.RemoveAll();
- }
- }
- public void DeleteNode(string Node)
- {
- //刪除一个节点。
- try
- {
- string mainNode = Node.Substring(0, Node.LastIndexOf("/"));
- objXmlDoc.SelectSingleNode(mainNode).RemoveChild(objXmlDoc.SelectSingleNode(Node));
- }
- catch
- {
- //throw;
- return;
- }
- }
- public void InsertNodeWithChild(string mainNode, string ChildNode, string Element, string Content)
- {
- //插入一节点和此节点的一子节点。
- XmlNode objRootNode = objXmlDoc.SelectSingleNode(mainNode);
- XmlElement objChildNode = objXmlDoc.CreateElement(ChildNode);
- objRootNode.AppendChild(objChildNode);//插入节点
- XmlElement objElement = objXmlDoc.CreateElement(Element);
- objElement.InnerText = Content;
- objChildNode.AppendChild(objElement);//插入子节点
- }
- /// <summary>
- /// 插入一个节点,带一个Attribute和innerText
- /// </summary>
- /// <param name="mainNode"></param>
- /// <param name="Element">节点名称</param>
- /// <param name="Attrib">Attribute名称</param>
- /// <param name="AttribContent">Attribute值</param>
- /// <param name="Content">innerText值</param>
- public void InsertNode(string mainNode, string Element, string Attrib, string AttribContent, string Content)
- {
- XmlNode objNode = objXmlDoc.SelectSingleNode(mainNode);
- XmlElement objElement = objXmlDoc.CreateElement(Element);
- objElement.SetAttribute(Attrib, AttribContent);
- objElement.InnerText = Content;
- objNode.AppendChild(objElement);
- }
- /// <summary>
- /// 插入一个节点,带一个Attribute
- /// </summary>
- /// <param name="mainNode"></param>
- /// <param name="Element">节点名称</param>
- /// <param name="Attrib">Attribute名称</param>
- /// <param name="AttribContent">Attribute值</param>
- public void InsertNode(string mainNode, string Element, string Attrib, string AttribContent)
- {
- XmlNode objNode = objXmlDoc.SelectSingleNode(mainNode);
- XmlElement objElement = objXmlDoc.CreateElement(Element);
- objElement.SetAttribute(Attrib, AttribContent);
- objNode.AppendChild(objElement);
- }
- /// <summary>
- /// 插入一个节点
- /// </summary>
- /// <param name="mainNode"></param>
- /// <param name="Element">节点名称</param>
- public void InsertNode(string mainNode, string Element)
- {
- XmlNode objNode = objXmlDoc.SelectSingleNode(mainNode);
- XmlElement objElement = objXmlDoc.CreateElement(Element);
- objNode.AppendChild(objElement);
- }
- //<summary>
- //插入一个节点,带多个属性和一个inner text
- //</summary>
- public void InsertNode(string mainNode, string elementName, string[] arrAttributeName, string[] arrAttributeContent, string elementContent)
- {
- try
- {
- XmlNode objNode = objXmlDoc.SelectSingleNode(mainNode);
- XmlElement objElement = objXmlDoc.CreateElement(elementName);
- for (int i = 0; i <= arrAttributeName.GetUpperBound(0); i++)
- {
- objElement.SetAttribute(arrAttributeName[i], arrAttributeContent[i]);
- }
- objElement.InnerText = elementContent;
- objNode.AppendChild(objElement);
- }
- catch
- {
- throw;
- //string t = mainNode;
- }
- }
- ///<summary>
- ///插入一个节点,带多个属性
- ///</summary>
- public void InsertNode(string mainNode, string elementName, string[] arrAttributeName, string[] arrAttributeContent)
- {
- try
- {
- XmlNode objNode = objXmlDoc.SelectSingleNode(mainNode);
- XmlElement objElement = objXmlDoc.CreateElement(elementName);
- for (int i = 0; i <= arrAttributeName.GetUpperBound(0); i++)
- {
- objElement.SetAttribute(arrAttributeName[i], arrAttributeContent[i]);
- }
- //objElement.InnerText = elementContent;
- objNode.AppendChild(objElement);
- }
- catch
- {
- throw;
- //string t = mainNode;
- }
- }
- /// <summary>
- /// 插入子节点(带多个属性)
- /// </summary>
- /// <param name="parentNode">要插入的父节点</param>
- /// <param name="elementName">插入的节点名称</param>
- /// <param name="arrAttributeName">属性名称[数组]</param>
- /// <param name="arrAttributeContent">属性内容[数组]</param>
- /// <param name="elementContent">节点内容</param>
- public void AddChildNode(string parentNodePath, string elementName, string[] arrAttributeName, string[] arrAttributeContent, string elementContent)
- {
- try
- {
- XmlNode parentNode = objXmlDoc.SelectSingleNode(parentNodePath);
- XmlElement objChildElement = objXmlDoc.CreateElement(elementName);
- for (int i = 0; i <= arrAttributeName.GetUpperBound(0); i++)
- {
- objChildElement.SetAttribute(arrAttributeName[i], arrAttributeContent[i]);
- }
- objChildElement.InnerText = elementContent;
- parentNode.AppendChild(objChildElement);
- }
- catch
- {
- return;
- }
- }
- /// <summary>
- /// 插入子节点(将内容以CData形式写入)
- /// </summary>
- /// <param name="parentNode">要插入的父节点</param>
- /// <param name="elementName">插入的节点名称</param>
- /// <param name="elementContent">节点内容</param>
- public void AddChildNodeCData(string parentNodePath, string elementName, string elementContent)
- {
- try
- {
- XmlNode parentNode = objXmlDoc.SelectSingleNode(parentNodePath);
- XmlElement objChildElement = objXmlDoc.CreateElement(elementName);
- //写入cData数据
- XmlCDataSection xcds = objXmlDoc.CreateCDataSection(elementContent);
- objChildElement.AppendChild(xcds);
- parentNode.AppendChild(objChildElement);
- }
- catch
- {
- return;
- }
- }
- /// <summary>
- /// 插入子节点(仅内容,不带属性)
- /// </summary>
- /// <param name="parentNode">要插入的父节点</param>
- /// <param name="elementName">插入的节点名称</param>
- /// <param name="elementContent">节点内容</param>
- public void AddChildNode(string parentNodePath, string elementName, string elementContent)
- {
- try
- {
- XmlNode parentNode = objXmlDoc.SelectSingleNode(parentNodePath);
- XmlElement objChildElement = objXmlDoc.CreateElement(elementName);
- objChildElement.InnerText = elementContent;
- parentNode.AppendChild(objChildElement);
- }
- catch
- {
- return;
- }
- }
- /// <summary>
- /// 根据xpath值查找节点
- /// </summary>
- /// <param name="NodePath">要查找节点的xpath值</param>
- /// <returns>找到返回true,否则返回true</returns>
- public bool FindNode(string NodePath)
- {
- try
- {
- if (objXmlDoc.SelectSingleNode(NodePath) != null)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- catch
- {
- return false;
- }
- }
XML操作:1.XML类(http://blog.csdn.net/happy09li/article/details/7460521)的更多相关文章
- golang操作memcached 转自https://blog.csdn.net/weixin_37696997/article/details/78760397
go使用memcached需要第三方的驱动库,这里有一个库是memcached作者亲自实现的,代码质量效率肯定会有保障 1:安装 go get github.com/bradfitz/gomemcac ...
- maven 依赖文件 pom.xml 编译 mvn compile 运行 不用mvn exec:java -Dexec.mainClass="hello.HelloWorld" 打成jar包 mvn package mvn install http://blog.csdn.net/yaya1943/article/details/48464371
使用maven编译Java项目 http://blog.csdn.net/yaya1943/article/details/48464371 使用"mvn clean"命令清除编 ...
- 集合各个实现类的底层实现原理 ----- 原文地址:https://blog.csdn.net/qq_25868207/article/details/55259978
ArrayList实现原理要点概括 参考文献: http://zhangshixi.iteye.com/blog/674856l https://www.cnblogs.com/leesf456/p/ ...
- JSON.parseObject将json字符串转换为bean类,是否大小写敏感区分---https://blog.csdn.net/mathlpz126/article/details/80684034
JSON.parseObject将json字符串转换为bean类,是否大小写敏感区分 https://blog.csdn.net/mathlpz126/article/details/80684034
- UML类图几种关系的总结(转载 http://blog.csdn.net/tianhai110/article/details/6339565 )
http://blog.csdn.net/tianhai110/article/details/6339565
- Java类和对象 详解(一)---写的很好通俗易懂---https://blog.csdn.net/wei_zhi/article/details/52745268
https://blog.csdn.net/wei_zhi/article/details/52745268
- RedHat如何关闭防火墙 : http://blog.csdn.net/chongxin1/article/details/76072758
版本号:RedHat6.5 JDK1.8 Hadoop2.7.3 hadoop 说明:从版本2开始加入了Yarn这个资源管理器,Yarn并不需要单独安装.只要在机器上安装了JDK就可以直接安 ...
- mybaits入门(含实例教程和源码) http://blog.csdn.net/u013142781/article/details/50388204
前言:mybatis是一个非常优秀的存储过程和高级映射的优秀持久层框架.大大简化了,数据库操作中的常用操作.下面将介绍mybatis的一些概念和在eclipse上的实际项目搭建使用. 一.mybati ...
- http://blog.csdn.net/LANGXINLEN/article/details/50421988
GitHub上史上最全的Android开源项目分类汇总 今天在看博客的时候,无意中发现了 @Trinea在GitHub上的一个项目 Android开源项目分类汇总, 由于类容太多了,我没有一个个完整地 ...
随机推荐
- bzoj1297: [SCOI2009]迷路
矩阵. 一个图的邻接矩阵的m次幂相当于 长度恰好为m的路径数.这要求边权为1. 因为边权小于等于9,所以可以把一个点拆成9的点. 拆成的第(i+1)个点向第i个点连边. 如果存在边(u,v,w) 就由 ...
- UVa 10652 (简单凸包) Board Wrapping
题意: 有n块互不重叠的矩形木板,用尽量小的凸多边形将它们包起来,并输出并输出木板总面积占凸多边形面积的百分比. 分析: 几乎是凸包和多边形面积的裸题. 注意:最后输出的百分号前面有个空格,第一次交P ...
- 原创:js代码, 让dedecms支持Tag选择, 添加内容更为方便,不用手输Tag
dedecms在编辑修改内容时,TAG标签需要手动输,中文的Tag, 中间还得用半角字符','分隔, 输入法切来切去很不方便, 于是动手改后台代码, 利用后台的tags_main.php, 让d ...
- php复制目录及文件
<?php /* 复制目录 */ function copydir($dirsrc,$dirto){ if(is_file($dirto)){ echo "目标不是目录不能创建&quo ...
- 聚焦 SQL 数据库活动异地复制
Tobias Ternstrom US-DS-PM 首席部门项目经理 本文作为一系列业务连续性和灾难恢复文章的开篇,概述了业务连续性的各种场景,然后重点介绍 SQL 数据库高级服务级别提供的活动异地 ...
- 【转】Windows环境下Android Studio v1.0安装教程
原文网址:http://ask.android-studio.org/?/article/9 http://android-studio.org/index.php/docs/experience/1 ...
- C# 类与接口的几个问题的总结(待续)
1. 有关类的多态性 C#中,类的多态性是通过在子类中重载基类的虚方法(virtual标识)或函数成员来实现. 在C#中,继承.虚方法和重写方法组合在一起才能实现多态性. 2. 显式接口成员的使用 显 ...
- 【九度OJ】题目1009-二叉搜索树
题目 思路 构建二叉搜索树,并保存先序遍历和中序遍历的序列在samplePreOrder,sampleInOrder 每遇到一个新的序列,构建一棵二叉搜索树,保存先序遍历和中序遍历的序列testPre ...
- CodeForce---Educational Codeforces Round 3 Load Balancing 正题
看到这题是我的想法就是肯定跟平均值有关但是接下来就不知道怎么做了 看完大神的正解数之后,原来发现是这么简单,但是就是不知道为啥一定是平均值和平均值加1,而不是平均值和平均值减1: 好啦下面就贴出大神的 ...
- C#相关图书推荐
C#入门经典(第6版) 作 者 [美] Karli Watson,Jacob Vibe Hammer,Jon D Reid 等 著: 出 版 社 清华大学出版社 出版时间 2014-01-0 ...