基本信息
源码名称:C#快速上手XML增删查改
源码大小:0.20M
文件格式:.zip
开发语言:C#
更新时间:2020-04-16
   源码介绍

这篇文章(https://www.daboke.com)主要介绍了使用C#快速上手XML的增删查改操作的方法。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;


namespace xmlSample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        DataSet dataSet = new DataSet();//声明此数据集,存储读取出的XML数据

        /// <summary>
        /// 创建XML的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button5_Click(object sender, EventArgs e)
        {
            //创建一个XML文档
            XmlDocument doc = new XmlDocument();
            //创建第一行描述信息
            XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
            //将创建的第一行描述信息添加到文档中
            doc.AppendChild(dec);

            //给文档添加根节点
            XmlElement Parameter = doc.CreateElement("Parameter");
            doc.AppendChild(Parameter);

            //添加子节点信息
            XmlElement Info = doc.CreateElement("Info");
            Info.SetAttribute("id", textBox1.Text);
            Parameter.AppendChild(Info);

            XmlElement Code = doc.CreateElement("Code");
            Code.InnerText = textBox1.Text;
            Info.AppendChild(Code);

            XmlElement Name = doc.CreateElement("Name");
            Name.InnerText = textBox2.Text;
            Info.AppendChild(Name);

            XmlElement Authority = doc.CreateElement("Authority");
            Authority.InnerText = textBox3.Text;
            Info.AppendChild(Authority);

            XmlElement Max = doc.CreateElement("Max");
            Max.InnerText = textBox4.Text;
            Info.AppendChild(Max);

            XmlElement Min = doc.CreateElement("Min");
            Min.InnerText = textBox5.Text;
            Info.AppendChild(Min);

            XmlElement Default = doc.CreateElement("Default");
            Default.InnerText = textBox6.Text;
            Info.AppendChild(Default);

            doc.Save("ParameterInfo.xml");
            showXML();
            MessageBox.Show("创建成功");
        }

        /// <summary>
        /// 添加子节点的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button1_Click(object sender, EventArgs e)
        {
            XmlDocument doc = new XmlDocument();
            //首先判断文件是否存在,如果存在则追加否则在创建一个
            if (File.Exists("ParameterInfo.xml"))
            {
                //加载
                doc.Load("ParameterInfo.xml");

                //获取根节点,给根节点添加子节点
                XmlElement Parameter = doc.DocumentElement;
                XmlElement Info = doc.CreateElement("Info");
                Info.SetAttribute("id", textBox7.Text);
                Parameter.AppendChild(Info);

                XmlElement Code = doc.CreateElement("Code");
                Code.InnerText = textBox7.Text;
                Info.AppendChild(Code);

                XmlElement Name = doc.CreateElement("Name");
                Name.InnerText = textBox8.Text;
                Info.AppendChild(Name);

                XmlElement Authority = doc.CreateElement("Authority");
                Authority.InnerText = textBox9.Text;
                Info.AppendChild(Authority);

                XmlElement Max = doc.CreateElement("Max");
                Max.InnerText = textBox10.Text;
                Info.AppendChild(Max);

                XmlElement Min = doc.CreateElement("Min");
                Min.InnerText = textBox11.Text;
                Info.AppendChild(Min);

                XmlElement Default = doc.CreateElement("Default");
                Default.InnerText = textBox12.Text;
                Info.AppendChild(Default);

                doc.Save("ParameterInfo.xml");
                showXML();
                MessageBox.Show("添加成功");
            }
            else
            {
                MessageBox.Show("尚未创建ParameterInfo文档,无法添加根节点!");
            }
        }

        /// <summary>
        /// 修改子节点的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button4_Click(object sender, EventArgs e)
        {

            XmlDocument doc = new XmlDocument();
            doc.Load("ParameterInfo.xml");
            XmlElement parameter = doc.DocumentElement;
            //根据ID修改其它值
            string id = textBox13.Text;
            XmlNode xn = parameter.SelectSingleNode("/Parameter/Info[@id="   id   "]");
            xn["Code"].InnerText = textBox13.Text;
            xn["Name"].InnerText = textBox14.Text;
            xn["Authority"].InnerText = textBox15.Text;
            xn["Max"].InnerText = textBox16.Text;
            xn["Min"].InnerText = textBox17.Text;
            xn["Default"].InnerText = textBox18.Text;

            doc.Save("ParameterInfo.xml");
            showXML();
            MessageBox.Show("修改成功!");
        }

        /// <summary>
        /// 删除子节点的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button2_Click(object sender, EventArgs e)
        {

            // 删除指定的行数据
            //获得当前选中行的ID
            string id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
            //根据ID进行删除
            XmlDocument doc = new XmlDocument();
            doc.Load("ParameterInfo.xml");
            XmlElement parameter = doc.DocumentElement;
            //获得要删除的节点
            XmlNode xn = parameter.SelectSingleNode("/Parameter/Info[@id="   id   "]");
            //通过父节点移除子节点,包括子节点下面的节点
            parameter.RemoveChild(xn);

            doc.Save("ParameterInfo.xml");
            showXML();
            MessageBox.Show("删除成功!");
        }

        /// <summary>
        /// dataGridView显示XML信息的方法
        /// </summary>
        private void showXML()
        {
            if (File.Exists("ParameterInfo.xml"))
            {
                //读取XML文件信息
                dataSet.Clear();
                dataSet.ReadXml("ParameterInfo.xml");
                dataGridView1.DataSource = dataSet.Tables[0].DefaultView;
            }
            else
            {
                ;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            showXML();
        }

        /// <summary>
        /// 查询XML信息的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button3_Click(object sender, EventArgs e)
        {
            XmlDocument doc = new XmlDocument();
            if (File.Exists("ParameterInfo.xml"))
            {
                doc.Load("ParameterInfo.xml");
                // 获取根节点
                XmlElement parameterElement = doc.DocumentElement;
                XmlNodeList parameterChildr = parameterElement.ChildNodes;
                foreach (XmlNode item in parameterChildr)
                {
                    textBox19.Text  = "节点名称:"   item.Name   "  节点的 InnerText :"   item.InnerText   "\r\n";
                }
            }
            else
            {
                MessageBox.Show("ParameterInfo.xml文件不存在!");
            }
            doc.Save("ParameterInfo.xml");
        }

        private void Button6_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("https://www.daboke.com");//欢迎访问大博客,探索更多编程实战案例!
        }

        private void Button7_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("https://www.daboke.com/program/xmlsample");//原文链接!
        }
    }
}