- 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;
- }
- }