C#创建、读写、增加、删除XML操作_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > C#创建、读写、增加、删除XML操作

C#创建、读写、增加、删除XML操作

 2017/8/26 15:08:48  JackMo  程序员俱乐部  我要评论(0)
  • 摘要:usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Xml;usingSystem.Collections;namespaceCommandXML{publicclasscmdXML{///<summary>///创建XML文件///</summary>///<
  • 标签:C# 创建 操作 XML操作 XML

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


    }
}

上一篇: .net项目架构改造之搭建基于java环境配置一览【上】 下一篇: 没有下一篇了!
发表评论
用户名: 匿名