XML操作
来源:互联网 发布:凯立德软件版本是什么 编辑:程序博客网 时间:2024/06/14 07:27
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;
- }
- 操作XML
- XML操作
- xml操作
- 操作XML
- xml 操作
- XML操作
- xml操作
- xml操作
- 操作XML
- XML 操作
- 操作XML
- XML操作
- XML 操作
- xml操作
- Xml操作
- xml操作
- xml操作
- xml操作
- asp.net 2.0中的弹出对话框
- YARN或将成为Hadoop新发力点
- asp.net 2.0中设定默认焦点按钮
- VIM 撤销与恢复
- asp.net 2.0中使用sitemapDATAsource做页面导航
- XML操作
- asp.net 2.0中TREEVIEW中动态增加结点
- 一个.net实现的ubb类
- ASP.NET 2.0中改变passwordrecovery发邮件时的信息
- asp.net 2.0中的Cross PAGE REQUEST
- Windows下的Eclipse启动出现:a java runtime environment(JRE) or java development kit(JDK) must be....
- cocos2d-x在Android真机上使用Sqlite
- Oracle对xml的支持
- 每个程序员都应该知道的论文和文章