马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

XML增加、删除、修改、查询 节点帮助类

[复制链接]
查看4075 | 回复1 | 2017-8-28 08:50:23 | 显示全部楼层 |阅读模式
XML帮助类 (增加、删除、修改、查询 )
[C#] 纯文本查看 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Collections;

namespace CommandXML
{
    public class cmdXML
    {


        /// <summary>
        /// 创建XML文件
        /// </summary>
        /// <param name="xmlFilePath">存放目录</param>
        /// <param name="rootNodename">根节点名字</param>
        public void CreateXMLDoc(string xmlFilePath, string rootNodename)
         {
             //初始化一个xml实例
             XmlDocument myXmlDoc = new XmlDocument();
             //<?xml version="1.0" encoding="UTF-8"?>
             myXmlDoc.AppendChild(myXmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null));
             //创建xml的根节点
             XmlElement rootElement = myXmlDoc.CreateElement(rootNodename);
             //将根节点加入到xml文件中(AppendChild)
             myXmlDoc.AppendChild(rootElement);
             myXmlDoc.Save(xmlFilePath);
         }


        /// <summary>
        /// 增加第一层的节点
        /// </summary>
        /// <param name="xmlFilePath">文件路径</param>
        /// <param name="RootPath">根节点名字</param>
        /// <param name="Name">所要添加第一层节点的节点名</param>
        /// <param name="attribute"></param>
        public void AddXmlFirstNode(string xmlFilePath, string RootPath, string Name, string[,] attribute)
         {
             try
             {
                 XmlDocument myXmlDoc = new XmlDocument();
                 myXmlDoc.Load(xmlFilePath);

                 XmlNode memberlist = myXmlDoc.SelectSingleNode(RootPath);
                 //XmlNodeList nodelist = memberlist.ChildNodes;

                 XmlElement firstLevelElement1 = myXmlDoc.CreateElement(Name);
                 //填充第一层的第一个子节点的属性值(SetAttribute)
                 for (int i = 0; i < attribute.GetLength(0); i++)
                 {
                     firstLevelElement1.SetAttribute(attribute[i, 0], attribute[i, 1]);
                 }
                 //将第一层的第一个子节点加入到根节点下
                 memberlist.AppendChild(firstLevelElement1);

                 //保存更改
                 myXmlDoc.Save(xmlFilePath);
             }
             catch (Exception ex)
             {
                 Console.WriteLine(ex.ToString());
             }
         }

         /// <summary>
         /// 增加第二层节点
         /// </summary>
         /// <param name="xmlFilePath">路径</param>
         /// <param name="RootPath">根节点名</param>
         /// <param name="FirstElementattributesName">第一层节点属性名</param>
         /// <param name="Firstattributes">第一层节点属性名对应的值</param>
         /// <param name="SecondElement">所要增加的第二层节点名</param>
         /// <param name="SecondinnerText">第二层节点对应的存储内容</param>
        public void AddXmlSecondNod(string xmlFilePath, string RootPath, string FirstElementattributesName, string Firstattributes, string[] SecondElement, string[] SecondinnerText)
         {
             try
             {
                 XmlDocument myXmlDoc = new XmlDocument();
                 myXmlDoc.Load(xmlFilePath);

                 XmlNode memberlist = myXmlDoc.SelectSingleNode(RootPath);
                 XmlNodeList nodelist = memberlist.ChildNodes;

                 //添加一个带有属性的节点信息
                 foreach (XmlNode node in nodelist)
                 {
                     if (node.Attributes[FirstElementattributesName].Value.Equals(Firstattributes))
                     {
                         for (int i = 0; i < SecondElement.Length; i++)
                         {
                             XmlElement newElement = myXmlDoc.CreateElement(SecondElement[i]);
                             newElement.InnerText = SecondinnerText[i];
                             node.AppendChild(newElement);
                         }
                     }
                 }
                 //保存更改
                 myXmlDoc.Save(xmlFilePath);
             }
             catch (Exception ex)
             {
                 Console.WriteLine(ex.ToString());
             }
         }


        /// <summary>
        /// 获取第一层节点的属性值,返回所有的属性名和对应的值
        /// </summary>
        /// <param name="xmlFilePath">文件路径</param>
        /// <param name="RootPath">根节点名</param>
        /// <param name="firstNodeName">第一层节点名</param>
        /// <returns></returns>
        public ArrayList GetXMLFirstNodeAttributes(string xmlFilePath, string RootPath, string firstNodeName)
         {
             ArrayList list = new ArrayList();
             try
             {
                 //初始化一个xml实例
                 XmlDocument myXmlDoc = new XmlDocument();
                 //加载xml文件(参数为xml文件的路径)
                 myXmlDoc.Load(xmlFilePath);
                 //获得第一个姓名匹配的节点(SelectSingleNode):此xml文件的根节点
                 XmlNode rootNode = myXmlDoc.SelectSingleNode(RootPath);
                 //分别获得该节点的InnerXml和OuterXml信息
                 string innerXmlInfo = rootNode.InnerXml.ToString();
                 string outerXmlInfo = rootNode.OuterXml.ToString();
                 //获得该节点的子节点(即:该节点的第一层子节点)
                 XmlNodeList firstLevelNodeList = rootNode.ChildNodes;
                 foreach (XmlNode node in firstLevelNodeList)
                 {
                     //获得该节点的属性集合
                     if (node.Name == firstNodeName)
                     {
                         XmlAttributeCollection attributeCol = node.Attributes;
                         foreach (XmlAttribute attri in attributeCol)
                         {
                             //获取属性名称与属性值
                             string name = attri.Name;
                             string value = attri.Value;
                             list.Add(name + ":" + value);
                         }
                     }
                 }
             }
             catch (Exception ex)
             {
                 Console.WriteLine(ex.ToString());
             }
             return list;
         }


        /// <summary>
        /// 获取第二层节点的存储值
        /// </summary>
        /// <param name="xmlFilePath">文件路径</param>
        /// <param name="RootPath">根节点</param>
        /// <param name="firstNodeName">第一层节点名</param>
        /// <param name="secondNoadeName">第二层节点名</param>
        /// <returns></returns>
        public ArrayList GetXMLSecondNodeValue(string xmlFilePath, string RootPath, string firstNodeName, string secondNoadeName)
         {
             ArrayList list = new ArrayList();
             try
             {
                 //初始化一个xml实例
                 XmlDocument myXmlDoc = new XmlDocument();
                 //加载xml文件(参数为xml文件的路径)
                 myXmlDoc.Load(xmlFilePath);
                 //获得第一个姓名匹配的节点(SelectSingleNode):此xml文件的根节点
                 XmlNode rootNode = myXmlDoc.SelectSingleNode(RootPath);
                 //分别获得该节点的InnerXml和OuterXml信息
                 string innerXmlInfo = rootNode.InnerXml.ToString();
                 string outerXmlInfo = rootNode.OuterXml.ToString();
                 //获得该节点的子节点(即:该节点的第一层子节点)
                 XmlNodeList firstLevelNodeList = rootNode.ChildNodes;
                 foreach (XmlNode node in firstLevelNodeList)
                 {
                     //获得该节点的属性集合
                     if (node.Name == firstNodeName)
                     {
                         foreach (XmlNode _node in node.ChildNodes)
                         {
                             if (_node.Name == secondNoadeName)
                                 list.Add(_node.InnerText);
                         }
                     }

                     //判断此节点是否还有子节点
                     if (node.HasChildNodes)
                     {
                         //获取该节点的第一个子节点
                         XmlNode secondLevelNode1 = node.FirstChild;
                         //获取该节点的名字
                         string name = secondLevelNode1.Name;
                         //获取该节点的值(即:InnerText)
                         string innerText = secondLevelNode1.InnerText;
                         Console.WriteLine("{0} = {1}", name, innerText);

                         //获取该节点的第二个子节点(用数组下标获取)
                         XmlNode secondLevelNode2 = node.ChildNodes[1];
                         name = secondLevelNode2.Name;
                         innerText = secondLevelNode2.InnerText;
                         Console.WriteLine("{0} = {1}", name, innerText);
                     }
                 }
             }
             catch (Exception ex)
             {
                 Console.WriteLine(ex.ToString());
             }
             return list;
         }


        /// <summary>
        /// 修改第一层节点的属性值
        /// </summary>
        /// <param name="xmlFilePath">文件路径</param>
        /// <param name="RootPath">根节点名</param>
        /// <param name="FirstNodeName">第一节点名</param>
        /// <param name="FirstNodeAttributes">第一节点属性名</param>
        /// <param name="FirstNodeAttributesOldValue">第一节点属性值</param>
        /// <param name="newValue"></param>
        public void ModifyXmlFirstattribute(string xmlFilePath, string RootPath, string FirstNodeName, string FirstNodeAttributes, string FirstNodeAttributesOldValue, string newValue)
         {
             try
             {
                 XmlDocument myXmlDoc = new XmlDocument();
                 myXmlDoc.Load(xmlFilePath);
                 //XmlNode rootNode = myXmlDoc.FirstChild;
                 //XmlNodeList firstLevelNodeList = rootNode.ChildNodes;
                 XmlNode rootNode = myXmlDoc.SelectSingleNode(RootPath);
                 foreach (XmlNode node in rootNode.ChildNodes)
                 {
                     if (node.Name.Equals(FirstNodeName))
                     {
                         //修改此节点的属性值
                         if (node.Attributes[FirstNodeAttributes].Value.Equals(FirstNodeAttributesOldValue))
                         {
                             node.Attributes[FirstNodeAttributes].Value = newValue;
                         }
                     }
                 }
                 //要想使对xml文件所做的修改生效,必须执行以下Save方法
                 myXmlDoc.Save(xmlFilePath);
             }
             catch (Exception ex)
             {
                 Console.WriteLine(ex.ToString());
             }
         }

        
        /// <summary>
        /// 修改第二节点的存储值
        /// </summary>
        /// <param name="xmlFilePath">文件路径</param>
        /// <param name="RootPath">根节点名字</param>
        /// <param name="FirstNodeName">第一节点名字</param>
        /// <param name="FirstNodeAttributes">第一节点属性名</param>
        /// <param name="FirstNodeAttributesValue">第一节点属性值</param>
        /// <param name="SecondNodeName">第二节点名字</param>
        /// <param name="value">第二节点存储值</param>
        public void ModifyXmlElementValue(string xmlFilePath, string RootPath, string FirstNodeName,string FirstNodeAttributes, string FirstNodeAttributesValue, string SecondNodeName, string value)
         {
             try
             {
                 XmlDocument myXmlDoc = new XmlDocument();
                 myXmlDoc.Load(xmlFilePath);
                 XmlNode rootNode = myXmlDoc.SelectSingleNode(RootPath);
                 foreach (XmlNode node in rootNode.ChildNodes)
                 {
                     if (node.Name.Equals(FirstNodeName))
                     {
                         //修改此节点的属性值
                         if (node.Attributes[FirstNodeAttributes].Value.Equals(FirstNodeAttributesValue))
                         {
                             foreach (XmlNode _node in node.ChildNodes)
                             {
                                 if (_node.Name == SecondNodeName)
                                 {
                                     _node.InnerText = value;
                                 }
                             }
                         }
                     }
                 }
                 //要想使对xml文件所做的修改生效,必须执行以下Save方法
                 myXmlDoc.Save(xmlFilePath);
             }
             catch (Exception ex)
             {
                 Console.WriteLine(ex.ToString());
             }
         }

 

        /// <summary>
        /// 删除第一节点
        /// </summary>
        /// <param name="xmlFilePath">路径</param>
        /// <param name="RootPath">根节点</param>
        /// <param name="FirstNodeName">第一节点名</param>
        /// <param name="FirstNodeAttributes">第一节点属性名</param>
        /// <param name="FirstNodeAttributesValue">第一节点属性值</param>
        public void DeleteXmlFirstnode(string xmlFilePath, string RootPath, string FirstNodeName, string FirstNodeAttributes, string FirstNodeAttributesValue)
         {
             try
             {
                 XmlDocument myXmlDoc = new XmlDocument();
                 myXmlDoc.Load(xmlFilePath);
                 XmlNode rootNode = myXmlDoc.SelectSingleNode(RootPath);

                 foreach (XmlNode node in rootNode.ChildNodes)
                 {
                     if (node.Name.Equals(FirstNodeName))
                     {
                         if (node.Attributes[FirstNodeAttributes].Value.Equals(FirstNodeAttributesValue))
                         {
                             //node.RemoveAll();
                             rootNode.RemoveChild(node);
                         }
                     }
                 }
                 //保存对xml文件所做的修改
                 myXmlDoc.Save(xmlFilePath);
             }
             catch (Exception ex)
             {
                 Console.WriteLine(ex.ToString());
             }
         }

        /// <summary>
         /// 删除子节点
        /// </summary>
        /// <param name="xmlFilePath">路径</param>
        /// <param name="FirstElementattributesName">第一节点属性名</param>
        /// <param name="Firstattributes">第一节点属性值</param>
        /// <param name="secondnodeName">子节点名称</param>
        public void DeleteXmlsecondNode(string xmlFilePath, string RootPath, string FirstNodeName, string FirstNodeAttributes, string FirstNodeAttributesValue, string secondnodeName)
         {
             try
             {
                 XmlDocument myXmlDoc = new XmlDocument();
                 myXmlDoc.Load(xmlFilePath);
                 XmlNode rootNode = myXmlDoc.SelectSingleNode(RootPath);

                 foreach (XmlNode node in rootNode.ChildNodes)
                 {
                     if (node.Name.Equals(FirstNodeName))
                     {
                         if (node.Attributes[FirstNodeAttributes].Value.Equals(FirstNodeAttributesValue))
                         {
                             foreach (XmlNode _node in node.ChildNodes)
                             {
                                 if (_node.Name == secondnodeName)
                                     //_node.RemoveAll();
                                     node.RemoveChild(_node);

                             }
                         }
                     }
                 }
                 //保存对xml文件所做的修改
                 myXmlDoc.Save(xmlFilePath);
             }
             catch (Exception ex)
             {
                 Console.WriteLine(ex.ToString());
             }
         }


    }
}


C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
梁思齐 | 2017-9-14 21:41:55 | 显示全部楼层
嗯,真好!感觉好详细。
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则