Operation of xml files based on c#

xml (eXtensible Markup Language), an extensible markup language, can be used to describe data. It is a very important file type that is often used in projects.
The following introduces some basic operations on xml documents based on c#.
Add a citation.

using System.Xml;           
using System.Xml.Linq; 
using System.Collections.Generic;
首先,创建一个空的xml文档。
    /// <summary>
        /// 创建一个xml文档
        /// </summary>
        /// <param name="FileName">路径文件名带后缀</param>
        /// <param name="root">根节点</param>
        public void CreateXML(string FileName, string root)
        {
    
    
            XmlDocument XDoc = new XmlDocument();
            XmlDeclaration declaration = XDoc.CreateXmlDeclaration("1.0", "UTF-8", "yes");
            XDoc.AppendChild(declaration);
            XmlElement elem = XDoc.CreateElement(root);
            XDoc.AppendChild(elem);
            XDoc.Save(FileName);
        }
 获取xml文档的根节点
   public string GetXmlRoot(string FileName)
        {
    
    
            XDocument doc = XDocument.Load(FileName);
            XElement root = doc.Root;
            return root.Name.ToString();
        }

Write a first-level node to the xml document

  public void WriteXML_FirstChildNode(string FileName, string FirstChildNode)
        {
    
    
            XmlDocument XDoc = new XmlDocument();
            XDoc.Load(FileName);
            //根节点
            XmlNode elem = XDoc.SelectSingleNode(GetXmlRoot(FileName));

            if (elem.SelectSingleNode(FirstChildNode) == null)
            {
    
    
                XmlElement elem1 = XDoc.CreateElement(FirstChildNode);
                elem.AppendChild(elem1);
            }
            XDoc.Save(FileName);
        }

Write the secondary node under the primary node

    public void WriteXML_SecondChildNode(string FileName, string FirstChildNode, string SecondChildNode, string SecondChildNodevalue)
        {
    
    
            XmlDocument XDoc = new XmlDocument();
            XDoc.Load(FileName);
            //根节点
            XmlNode elem = XDoc.SelectSingleNode(GetXmlRoot(FileName));
            XmlNode elem2 = elem.SelectSingleNode(FirstChildNode);

            if (elem2.SelectSingleNode(SecondChildNode) == null)
            {
    
    
                XmlElement elem1 = XDoc.CreateElement(SecondChildNode);
                elem2.AppendChild(elem1);
            }
            XmlNode ele3 = elem2.SelectSingleNode(SecondChildNode);
            ele3.InnerText = SecondChildNodevalue;
            XDoc.Save(FileName);
        }

Delete a first-level node

    public void RemoveXml_FirstChildNode(string FileName, string FirstChildNode)
        {
    
    
            XmlDocument XDoc = new XmlDocument();
            XDoc.Load(FileName);
            //根节点
            XmlNode elem = XDoc.SelectSingleNode(GetXmlRoot(FileName));
            XmlNode elem1 = elem.SelectSingleNode(FirstChildNode);
            elem.RemoveChild(elem1);
            XDoc.Save(FileName);
        }

Delete every second-level node under the first-level node

    public void RemoveXml_SecondChildNode(string FileName, string FirstChildNode, string SecondChildNode)
        {
    
    
            XmlDocument XDoc = new XmlDocument();
            XDoc.Load(FileName);
            XmlNode elem = XDoc.SelectSingleNode(GetXmlRoot(FileName));
            XmlNode elem1 = elem.SelectSingleNode(FirstChildNode);
            XmlNode elem2 = elem1.SelectSingleNode(SecondChildNode);
            elem1.RemoveChild(elem2);
            XDoc.Save(FileName);
        }

Read the entire xml document and display it in the listbox

      public void ReadXml_All(string FileName, ListBox listBox)
        {
    
    
            listBox.Items.Clear();
            listBox.Items.Add("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>");
            XDocument doc = XDocument.Load(FileName);
            XElement root = doc.Root;
            listBox.Items.Add("<" + root.Name + ">");
            IEnumerable<XElement> listnode = root.Elements();

            foreach (XElement item in listnode)
            {
    
    
                listBox.Items.Add("  <" + item.Name + ">");
                var nodes = item.Elements();
                foreach (var noddd in nodes)
                {
    
    
                    listBox.Items.Add("       " + noddd);
                }
                listBox.Items.Add("  </" + item.Name + ">");
            }
            listBox.Items.Add("</" + root.Name + ">");
        }

Get the value of a second-level node under the first-level node

     public void ReadOne_SecondChildNodevalue(string FileName, string FirstChildNode, string SecondChildNode, out string SecondChildNodevalue)
        {
    
    
            SecondChildNodevalue = string.Empty;
            XmlDocument XDoc = new XmlDocument();
            XDoc.Load(FileName);
            //根节点
            XmlNode elem = XDoc.SelectSingleNode(GetXmlRoot(FileName));
            //一级子节点
            XmlNode elem1 = elem.SelectSingleNode(FirstChildNode);
            XmlNode elem2 = elem1.SelectSingleNode(SecondChildNode);
            if (elem2 == null)
            {
    
    
                MessageBox.Show("无此节点", "错误");
            }
            else
            {
    
    
                SecondChildNodevalue = elem2.InnerText.ToString();
            }
        }

Modify the value of a second-level node under the first-level node

        public void ReadOne_SecondChildNodeExvalue(string FileName, string FirstChildNode, string SecondChildNode, string SecondChildNodeValue)
        {
    
    
            XmlDocument XDoc = new XmlDocument();
            XDoc.Load(FileName);
            //根节点
            XmlNode elem = XDoc.SelectSingleNode(GetXmlRoot(FileName));
            //一级子节点
            XmlNode elem1 = elem.SelectSingleNode(FirstChildNode);
            XmlNode elem2 = elem1.SelectSingleNode(SecondChildNode);
            elem2.InnerText = SecondChildNodeValue;
            XDoc.Save(FileName);            
        }

Guess you like

Origin blog.csdn.net/Iawfy_/article/details/113649693