广告
返回顶部
首页 > 资讯 > 后端开发 > 其他教程 >C#实现XML文件操作详解
  • 737
分享到

C#实现XML文件操作详解

C#XML文件操作C#XML操作C#XML 2022-12-23 09:12:25 737人浏览 薄情痞子
摘要

目录实践过程效果代码实践过程 效果 代码 public partial class FORM1 : Form { public Form1() {

实践过程

效果

代码

public partial class FORM1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    static public string strName = "";//记录读取时XML文件路径
    static public string strOne = "";//定义两个变量保存文本框内的值
    static public string strTwo = "";
    static public string strThree = "";//保存子标记名称
    static public string strFour = "";//保存第二个子节点的属性名
    static public string strFive = "";//保存第二个节点的属性值
    static public string strSix = "";//保存节点路径

    private XmlDocument xmlDocument = new XmlDocument();
    private Xmlnode xmlNode;
    private XmlElement xmlElement;
    DataSet dataSet = new DataSet();//声明此数据集,存储读取出的XML数据

    private void Form1_Load(object sender, EventArgs e)
    {
        strName = "fileTwo.xml";
        if (File.Exists(strName))
        {
            ShowXml();
            button3.Enabled = false;
        }
        else
        {
            button3.Enabled = true;
        }
    }

    //修改创建的XML文件
    private void button7_Click(object sender, EventArgs e)
    {
        //修改第一个节点的属性
        xmlDocument.Load(strName);
        XmlNode nodeOne = xmlDocument.SelectSingleNode("//" + strThree);
        XmlElement ElementOne = (XmlElement)nodeOne;
        ElementOne.SetAttribute(attribute.Text, textBox4.Text);

        //修改第一个节点的值            
        XmlNode nodeTwo = xmlDocument.SelectSingleNode("//" + strThree + "/*");
        XmlElement ElementTwo = (XmlElement)nodeTwo;
        ElementTwo.InnerText = nodeContent.Text;

        //修改第二个节点的属性值
        XmlNode mainNodeThree = xmlDocument.SelectSingleNode("//" + textBox7.Text + "[@" + strFour + "='" + strFive + "']");
        XmlElement ElementThree = (XmlElement)mainNodeThree;
        ElementThree.SetAttribute(textBox10.Text, textBox8.Text);

        //修改第二个节点的值
        XmlNode nodeFour = xmlDocument.SelectSingleNode("//" + textBox7.Text + "[@" + strFour + "='" + textBox8.Text + "']/*");
        XmlElement ElementFour = (XmlElement)nodeFour;
        ElementFour.InnerText = textBox11.Text;

        xmlDocument.Save(strName);
        MessageBox.Show("恭喜你,修改成功!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        ShowXml();
    }

    private void dataGridView1_SelectionChanged(object sender, EventArgs e)
    {
        //修改文本框的ReadOnly属性
        readOnlytrue();
        //选定DataGridView中的一条记录在文本框内显示
        for (int i = 0; i < dataGridView1.Rows.Count; i++)  //遍历DataGridView中的每一行信息
        {
            if (dataGridView1.Rows[i].Selected)//判断该行是否被选中
            {
                if (i <= 0)
                {
                    nodeContent.Text = dataGridView1.Rows[i].Cells[childNode.Text].Value.ToString();  //在文本框内显示XML文件内的信息
                    textBox4.Text = dataGridView1.Rows[i].Cells[attribute.Text].Value.ToString();
                    strOne = nodeContent.Text;
                }
                if (i > 0)
                {
                    textBox11.Text = dataGridView1.Rows[i].Cells[childNode.Text].Value.ToString();
                    textBox8.Text = dataGridView1.Rows[i].Cells[attribute.Text].Value.ToString();
                    strTwo = textBox11.Text;
                    strFive = textBox8.Text;
                }
            }
        }
        //从指定路径加载XML文档
        xmlDocument.Load(strName);

        //从根节点开始读取
        XmlNode xNode = (XmlNode)xmlDocument.DocumentElement;
        root.Text = xNode.Name;

        //读取子节点的属性及内容
        XmlNode nodeTwo = xmlDocument.SelectSingleNode(root.Text + "/*");
        firstNode.Text = nodeTwo.Name;
        textBox7.Text = firstNode.Text;
        strThree = firstNode.Text;


        //读取次子节点的名称及内容
        XmlNode nodeThree = xmlDocument.SelectSingleNode(root.Text + "/" + firstNode.Text + "/*");
        childNode.Text = nodeThree.Name;
        textBox9.Text = childNode.Text;

        //读取指定节点的属性名
        XmlNode nodeOne = xmlDocument.SelectSingleNode("//" + firstNode.Text);
        XmlAttributeCollection xmlAttribute = nodeOne.Attributes;
        for (int i = 0; i < xmlAttribute.Count; i++)
        {
            if (i == 0)
            {
                attribute.Text = xmlAttribute.Item(i).Name;
                textBox10.Text = attribute.Text;
                strFour = textBox10.Text;
            }
        }
    }

    //自定义一个方法更改文本框的只读属性
    private void readOnlytrue()
    {
        root.ReadOnly = true;
        childNode.ReadOnly = true;
        attribute.ReadOnly = true;
        firstNode.ReadOnly = true;
        textBox7.ReadOnly = true;
        textBox9.ReadOnly = true;
        textBox10.ReadOnly = true;
    }

    //选定DataGridView中的数据,进行删除操作
    private void button2_Click(object sender, EventArgs e)
    {
        //判断是否有选中行
        if (dataGridView1.SelectedRows.Count != 0)
        {
            xmlDocument.Load(strName);
            //通过循环获取选中行的XPath
            for (int i = 0; i < dataGridView1.Rows.Count; i++)  //遍历DataGridView中的每一行信息
            {
                if (dataGridView1.Rows[i].Selected)//判断该行是否被选中
                {
                    string attriString = dataGridView1.Rows[i].Cells[attribute.Text].Value.ToString();
                    strSix = root.Text + "/" + strThree + "[@" + strFour + "='" + attriString + "']";//被选中节点的XPath
                }
            }

            string mainNode = strSix.Substring(0, strSix.LastIndexOf("/"));
            xmlDocument.SelectSingleNode(mainNode).RemoveChild(xmlDocument.SelectSingleNode(strSix));//从XML文件下移除节点的语句
            xmlDocument.Save(strName);
            ShowXml();
        }
        else
        {
            MessageBox.Show("暂无选中行,不能进行删除操作!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
    }

    //添加记录到当前节点下
    private void button1_Click(object sender, EventArgs e)
    {
        if (textBox6.Text != "")
        {
            xmlDocument.Load(strName);

            //添加节点的属性及属性值
            XmlNode nodeOne = xmlDocument.SelectSingleNode(root.Text);
            XmlElement elementOne = xmlDocument.CreateElement(textBox6.Text);
            elementOne.SetAttribute(textBox3.Text, textBox1.Text);
            nodeOne.AppendChild(elementOne);

            //添加当前节点的子节点及节点内容
            XmlNode nodeTwo = xmlDocument.SelectSingleNode("//" + textBox6.Text + "[@" + strFour + "='" + textBox1.Text + "']");
            XmlElement elementTwo = xmlDocument.CreateElement(textBox2.Text);
            elementTwo.InnerText = textBox5.Text;
            nodeTwo.AppendChild(elementTwo);

            xmlDocument.Save(strName);
            MessageBox.Show("节点添加成功!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            ShowXml();
        }
        else
        {
            MessageBox.Show("节点内容不能为空!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
    }

    //读取XML文件
    public void ShowXml()
    {
        //读取XML文件信息
        dataSet.Clear();
        dataSet.ReadXml(strName);
        dataGridView1.DataSource = dataSet.Tables[0].DefaultView;
    }
    //创建一个XML文档
    private void button3_Click(object sender, EventArgs e)
    {
        //添加XML的声明部分
        xmlNode = xmlDocument.CreateNode(XmlNodeType.XmlDeclaration, "", "");
        xmlDocument.AppendChild(xmlNode);

        //添加一个根节点
        xmlElement = xmlDocument.CreateElement("", root.Text, "");
        xmlDocument.AppendChild(xmlElement);

        //添加一个带属性值的次根节点
        XmlNode mainNode = xmlDocument.SelectSingleNode(root.Text);
        XmlElement ElementTwo = xmlDocument.CreateElement(firstNode.Text);
        ElementTwo.SetAttribute(attribute.Text, textBox4.Text);
        mainNode.AppendChild(ElementTwo);

        //在当前节点添加一个仅带值的节点
        XmlNode mainNodeOne = xmlDocument.SelectSingleNode(@root.Text + "/" + firstNode.Text);
        XmlElement ElementThree = xmlDocument.CreateElement(childNode.Text);
        ElementThree.InnerText = nodeContent.Text;
        mainNodeOne.AppendChild(ElementThree);

        //在根节点下,添加第二个子节点
        XmlNode mainNodeTwo = xmlDocument.SelectSingleNode(root.Text);
        XmlElement ElementFour = xmlDocument.CreateElement(textBox7.Text);
        ElementFour.SetAttribute(textBox10.Text, textBox8.Text);
        mainNodeTwo.AppendChild(ElementFour);
        strFour = textBox10.Text;

        //在当前节点添加一个仅带值的节点
        XmlNode mainNodeThree = xmlDocument.SelectSingleNode("//" + textBox7.Text + "[@" + strFour + "='" + textBox8.Text + "']");
        XmlElement ElementFive = xmlDocument.CreateElement(textBox9.Text);
        ElementFive.InnerText = textBox11.Text;
        mainNodeThree.AppendChild(ElementFive);

        SaveFileDialog saveDialog = new SaveFileDialog();
        saveDialog.Filter = "XML文件(*.XML)|*.XML";
        if (saveDialog.ShowDialog() == DialogResult.OK)
        {
            xmlDocument.Save(saveDialog.FileName);
        }
    }
}
partial class Form1
{
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region windows 窗体设计器生成的代码

    /// <summary>
    /// 设计器支持所需的方法 - 不要
    /// 使用代码编辑器修改此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
        this.groupBox5 = new System.Windows.Forms.GroupBox();
        this.dataGridView1 = new System.Windows.Forms.DataGridView();
        this.label18 = new System.Windows.Forms.Label();
        this.root = new System.Windows.Forms.TextBox();
        this.textBox4 = new System.Windows.Forms.TextBox();
        this.firstNode = new System.Windows.Forms.TextBox();
        this.childNode = new System.Windows.Forms.TextBox();
        this.attribute = new System.Windows.Forms.TextBox();
        this.groupBox6 = new System.Windows.Forms.GroupBox();
        this.label20 = new System.Windows.Forms.Label();
        this.nodeContent = new System.Windows.Forms.TextBox();
        this.label17 = new System.Windows.Forms.Label();
        this.label19 = new System.Windows.Forms.Label();
        this.label16 = new System.Windows.Forms.Label();
        this.label15 = new System.Windows.Forms.Label();
        this.groupBox2 = new System.Windows.Forms.GroupBox();
        this.button2 = new System.Windows.Forms.Button();
        this.button1 = new System.Windows.Forms.Button();
        this.button7 = new System.Windows.Forms.Button();
        this.label5 = new System.Windows.Forms.Label();
        this.label2 = new System.Windows.Forms.Label();
        this.groupBox1 = new System.Windows.Forms.GroupBox();
        this.textBox1 = new System.Windows.Forms.TextBox();
        this.textBox6 = new System.Windows.Forms.TextBox();
        this.textBox2 = new System.Windows.Forms.TextBox();
        this.textBox3 = new System.Windows.Forms.TextBox();
        this.label4 = new System.Windows.Forms.Label();
        this.textBox5 = new System.Windows.Forms.TextBox();
        this.label3 = new System.Windows.Forms.Label();
        this.label1 = new System.Windows.Forms.Label();
        this.label9 = new System.Windows.Forms.Label();
        this.label8 = new System.Windows.Forms.Label();
        this.groupBox4 = new System.Windows.Forms.GroupBox();
        this.textBox7 = new System.Windows.Forms.TextBox();
        this.button3 = new System.Windows.Forms.Button();
        this.label23 = new System.Windows.Forms.Label();
        this.label22 = new System.Windows.Forms.Label();
        this.label21 = new System.Windows.Forms.Label();
        this.label10 = new System.Windows.Forms.Label();
        this.textBox11 = new System.Windows.Forms.TextBox();
        this.textBox10 = new System.Windows.Forms.TextBox();
        this.textBox9 = new System.Windows.Forms.TextBox();
        this.textBox8 = new System.Windows.Forms.TextBox();
        this.groupBox5.SuspendLayout();
        ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
        this.groupBox6.SuspendLayout();
        this.groupBox2.SuspendLayout();
        this.groupBox1.SuspendLayout();
        this.groupBox4.SuspendLayout();
        this.SuspendLayout();
        // 
        // groupBox5
        // 
        this.groupBox5.Controls.Add(this.dataGridView1);
        this.groupBox5.Location = new System.Drawing.Point(12, 134);
        this.groupBox5.Name = "groupBox5";
        this.groupBox5.Size = new System.Drawing.Size(748, 161);
        this.groupBox5.TabIndex = 1313;
        this.groupBox5.TabStop = false;
        this.groupBox5.Text = "显示XML文件内容";
        // 
        // dataGridView1
        // 
        this.dataGridView1.AllowUserToAddRows = false;
        this.dataGridView1.BackgroundColor = System.Drawing.SystemColors.ActiveCaption;
        this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
        this.dataGridView1.Location = new System.Drawing.Point(2, 12);
        this.dataGridView1.MultiSelect = false;
        this.dataGridView1.Name = "dataGridView1";
        this.dataGridView1.RowTemplate.Height = 23;
        this.dataGridView1.Size = new System.Drawing.Size(745, 151);
        this.dataGridView1.TabIndex = 1306;
        this.dataGridView1.SelectionChanged += new System.EventHandler(this.dataGridView1_SelectionChanged);
        // 
        // label18
        // 
        this.label18.AutoSize = true;
        this.label18.Location = new System.Drawing.Point(9, 17);
        this.label18.Name = "label18";
        this.label18.Size = new System.Drawing.Size(65, 12);
        this.label18.TabIndex = 1308;
        this.label18.Text = "初始节点:";
        // 
        // root
        // 
        this.root.Location = new System.Drawing.Point(80, 14);
        this.root.Name = "root";
        this.root.Size = new System.Drawing.Size(100, 21);
        this.root.TabIndex = 0;
        // 
        // textBox4
        // 
        this.textBox4.Location = new System.Drawing.Point(266, 42);
        this.textBox4.Name = "textBox4";
        this.textBox4.Size = new System.Drawing.Size(100, 21);
        this.textBox4.TabIndex = 3;
        // 
        // firstNode
        // 
        this.firstNode.Location = new System.Drawing.Point(265, 12);
        this.firstNode.Name = "firstNode";
        this.firstNode.Size = new System.Drawing.Size(100, 21);
        this.firstNode.TabIndex = 1;
        // 
        // childNode
        // 
        this.childNode.Location = new System.Drawing.Point(80, 70);
        this.childNode.Name = "childNode";
        this.childNode.Size = new System.Drawing.Size(100, 21);
        this.childNode.TabIndex = 4;
        // 
        // attribute
        // 
        this.attribute.Location = new System.Drawing.Point(80, 42);
        this.attribute.Name = "attribute";
        this.attribute.Size = new System.Drawing.Size(100, 21);
        this.attribute.TabIndex = 2;
        // 
        // groupBox6
        // 
        this.groupBox6.Controls.Add(this.label18);
        this.groupBox6.Controls.Add(this.root);
        this.groupBox6.Controls.Add(this.textBox4);
        this.groupBox6.Controls.Add(this.firstNode);
        this.groupBox6.Controls.Add(this.childNode);
        this.groupBox6.Controls.Add(this.attribute);
        this.groupBox6.Controls.Add(this.label20);
        this.groupBox6.Controls.Add(this.nodeContent);
        this.groupBox6.Controls.Add(this.label17);
        this.groupBox6.Controls.Add(this.label19);
        this.groupBox6.Controls.Add(this.label16);
        this.groupBox6.Controls.Add(this.label15);
        this.groupBox6.Location = new System.Drawing.Point(12, 12);
        this.groupBox6.Name = "groupBox6";
        this.groupBox6.Size = new System.Drawing.Size(371, 116);
        this.groupBox6.TabIndex = 1310;
        this.groupBox6.TabStop = false;
        this.groupBox6.Text = "节点一";
        // 
        // label20
        // 
        this.label20.AutoSize = true;
        this.label20.Location = new System.Drawing.Point(195, 45);
        this.label20.Name = "label20";
        this.label20.Size = new System.Drawing.Size(53, 12);
        this.label20.TabIndex = 1315;
        this.label20.Text = "属性值:";
        // 
        // nodeContent
        // 
        this.nodeContent.Location = new System.Drawing.Point(266, 70);
        this.nodeContent.Name = "nodeContent";
        this.nodeContent.Size = new System.Drawing.Size(100, 21);
        this.nodeContent.TabIndex = 5;
        // 
        // label17
        // 
        this.label17.AutoSize = true;
        this.label17.Location = new System.Drawing.Point(195, 17);
        this.label17.Name = "label17";
        this.label17.Size = new System.Drawing.Size(53, 12);
        this.label17.TabIndex = 1310;
        this.label17.Text = "子标记:";
        // 
        // label19
        // 
        this.label19.AutoSize = true;
        this.label19.Location = new System.Drawing.Point(189, 76);
        this.label19.Name = "label19";
        this.label19.Size = new System.Drawing.Size(65, 12);
        this.label19.TabIndex = 1313;
        this.label19.Text = "标记内容:";
        // 
        // label16
        // 
        this.label16.AutoSize = true;
        this.label16.Location = new System.Drawing.Point(9, 45);
        this.label16.Name = "label16";
        this.label16.Size = new System.Drawing.Size(65, 12);
        this.label16.TabIndex = 1311;
        this.label16.Text = "属性名称:";
        // 
        // label15
        // 
        this.label15.AutoSize = true;
        this.label15.Location = new System.Drawing.Point(9, 75);
        this.label15.Name = "label15";
        this.label15.Size = new System.Drawing.Size(65, 12);
        this.label15.TabIndex = 1312;
        this.label15.Text = "次子标记:";
        // 
        // groupBox2
        // 
        this.groupBox2.Controls.Add(this.button2);
        this.groupBox2.Controls.Add(this.button1);
        this.groupBox2.Controls.Add(this.button7);
        this.groupBox2.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
        this.groupBox2.Location = new System.Drawing.Point(399, 301);
        this.groupBox2.Name = "groupBox2";
        this.groupBox2.Size = new System.Drawing.Size(360, 110);
        this.groupBox2.TabIndex = 1312;
        this.groupBox2.TabStop = false;
        this.groupBox2.Text = "操作类型";
        // 
        // button2
        // 
        this.button2.Location = new System.Drawing.Point(277, 37);
        this.button2.Name = "button2";
        this.button2.Size = new System.Drawing.Size(63, 23);
        this.button2.TabIndex = 15;
        this.button2.Text = "删除记录";
        this.button2.UseVisualStyleBackColor = true;
        this.button2.Click += new System.EventHandler(this.button2_Click);
        // 
        // button1
        // 
        this.button1.Location = new System.Drawing.Point(34, 37);
        this.button1.Name = "button1";
        this.button1.Size = new System.Drawing.Size(63, 23);
        this.button1.TabIndex = 14;
        this.button1.Text = "添加记录";
        this.button1.UseVisualStyleBackColor = true;
        this.button1.Click += new System.EventHandler(this.button1_Click);
        // 
        // button7
        // 
        this.button7.Location = new System.Drawing.Point(158, 37);
        this.button7.Name = "button7";
        this.button7.Size = new System.Drawing.Size(63, 23);
        this.button7.TabIndex = 13;
        this.button7.Text = "修改记录";
        this.button7.UseVisualStyleBackColor = true;
        this.button7.Click += new System.EventHandler(this.button7_Click);
        // 
        // label5
        // 
        this.label5.AutoSize = true;
        this.label5.Location = new System.Drawing.Point(16, 21);
        this.label5.Name = "label5";
        this.label5.Size = new System.Drawing.Size(53, 12);
        this.label5.TabIndex = 1316;
        this.label5.Text = "子标记:";
        // 
        // label2
        // 
        this.label2.AutoSize = true;
        this.label2.Location = new System.Drawing.Point(6, 49);
        this.label2.Name = "label2";
        this.label2.Size = new System.Drawing.Size(65, 12);
        this.label2.TabIndex = 1312;
        this.label2.Text = "次子标记:";
        // 
        // groupBox1
        // 
        this.groupBox1.Controls.Add(this.label5);
        this.groupBox1.Controls.Add(this.label2);
        this.groupBox1.Controls.Add(this.textBox1);
        this.groupBox1.Controls.Add(this.textBox6);
        this.groupBox1.Controls.Add(this.textBox2);
        this.groupBox1.Controls.Add(this.textBox3);
        this.groupBox1.Controls.Add(this.label4);
        this.groupBox1.Controls.Add(this.textBox5);
        this.groupBox1.Controls.Add(this.label3);
        this.groupBox1.Controls.Add(this.label1);
        this.groupBox1.Location = new System.Drawing.Point(14, 301);
        this.groupBox1.Name = "groupBox1";
        this.groupBox1.Size = new System.Drawing.Size(369, 110);
        this.groupBox1.TabIndex = 1314;
        this.groupBox1.TabStop = false;
        this.groupBox1.Text = "添加节点";
        // 
        // textBox1
        // 
        this.textBox1.Location = new System.Drawing.Point(261, 46);
        this.textBox1.Name = "textBox1";
        this.textBox1.Size = new System.Drawing.Size(100, 21);
        this.textBox1.TabIndex = 102;
        // 
        // textBox6
        // 
        this.textBox6.Location = new System.Drawing.Point(89, 15);
        this.textBox6.Name = "textBox6";
        this.textBox6.Size = new System.Drawing.Size(100, 21);
        this.textBox6.TabIndex = 100;
        // 
        // textBox2
        // 
        this.textBox2.Location = new System.Drawing.Point(89, 46);
        this.textBox2.Name = "textBox2";
        this.textBox2.Size = new System.Drawing.Size(100, 21);
        this.textBox2.TabIndex = 103;
        // 
        // textBox3
        // 
        this.textBox3.Location = new System.Drawing.Point(261, 15);
        this.textBox3.Name = "textBox3";
        this.textBox3.Size = new System.Drawing.Size(100, 21);
        this.textBox3.TabIndex = 101;
        // 
        // label4
        // 
        this.label4.AutoSize = true;
        this.label4.Location = new System.Drawing.Point(202, 49);
        this.label4.Name = "label4";
        this.label4.Size = new System.Drawing.Size(53, 12);
        this.label4.TabIndex = 1315;
        this.label4.Text = "属性值:";
        // 
        // textBox5
        // 
        this.textBox5.Location = new System.Drawing.Point(89, 74);
        this.textBox5.Name = "textBox5";
        this.textBox5.Size = new System.Drawing.Size(100, 21);
        this.textBox5.TabIndex = 104;
        // 
        // label3
        // 
        this.label3.AutoSize = true;
        this.label3.Location = new System.Drawing.Point(12, 80);
        this.label3.Name = "label3";
        this.label3.Size = new System.Drawing.Size(65, 12);
        this.label3.TabIndex = 1313;
        this.label3.Text = "标记内容:";
        // 
        // label1
        // 
        this.label1.AutoSize = true;
        this.label1.Location = new System.Drawing.Point(190, 18);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(65, 12);
        this.label1.TabIndex = 1311;
        this.label1.Text = "属性名称:";
        // 
        // label9
        // 
        this.label9.AutoSize = true;
        this.label9.Location = new System.Drawing.Point(17, 17);
        this.label9.Name = "label9";
        this.label9.Size = new System.Drawing.Size(53, 12);
        this.label9.TabIndex = 1316;
        this.label9.Text = "子标记:";
        // 
        // label8
        // 
        this.label8.AutoSize = true;
        this.label8.Location = new System.Drawing.Point(391, 20);
        this.label8.Name = "label8";
        this.label8.Size = new System.Drawing.Size(0, 12);
        this.label8.TabIndex = 1309;
        // 
        // groupBox4
        // 
        this.groupBox4.Controls.Add(this.label9);
        this.groupBox4.Controls.Add(this.label8);
        this.groupBox4.Controls.Add(this.textBox7);
        this.groupBox4.Controls.Add(this.button3);
        this.groupBox4.Controls.Add(this.label23);
        this.groupBox4.Controls.Add(this.label22);
        this.groupBox4.Controls.Add(this.label21);
        this.groupBox4.Controls.Add(this.label10);
        this.groupBox4.Controls.Add(this.textBox11);
        this.groupBox4.Controls.Add(this.textBox10);
        this.groupBox4.Controls.Add(this.textBox9);
        this.groupBox4.Controls.Add(this.textBox8);
        this.groupBox4.Location = new System.Drawing.Point(384, 12);
        this.groupBox4.Name = "groupBox4";
        this.groupBox4.Size = new System.Drawing.Size(376, 116);
        this.groupBox4.TabIndex = 1311;
        this.groupBox4.TabStop = false;
        this.groupBox4.Text = "节点二";
        // 
        // textBox7
        // 
        this.textBox7.Location = new System.Drawing.Point(90, 11);
        this.textBox7.Name = "textBox7";
        this.textBox7.Size = new System.Drawing.Size(100, 21);
        this.textBox7.TabIndex = 6;
        // 
        // button3
        // 
        this.button3.Location = new System.Drawing.Point(319, 80);
        this.button3.Name = "button3";
        this.button3.Size = new System.Drawing.Size(51, 23);
        this.button3.TabIndex = 11;
        this.button3.Text = "创建";
        this.button3.UseVisualStyleBackColor = true;
        this.button3.Click += new System.EventHandler(this.button3_Click);
        // 
        // label23
        // 
        this.label23.AutoSize = true;
        this.label23.Location = new System.Drawing.Point(211, 45);
        this.label23.Name = "label23";
        this.label23.Size = new System.Drawing.Size(53, 12);
        this.label23.TabIndex = 1315;
        this.label23.Text = "属性值:";
        // 
        // label22
        // 
        this.label22.AutoSize = true;
        this.label22.Location = new System.Drawing.Point(13, 76);
        this.label22.Name = "label22";
        this.label22.Size = new System.Drawing.Size(65, 12);
        this.label22.TabIndex = 1313;
        this.label22.Text = "标记内容:";
        // 
        // label21
        // 
        this.label21.AutoSize = true;
        this.label21.Location = new System.Drawing.Point(7, 45);
        this.label21.Name = "label21";
        this.label21.Size = new System.Drawing.Size(65, 12);
        this.label21.TabIndex = 1312;
        this.label21.Text = "次子标记:";
        // 
        // label10
        // 
        this.label10.AutoSize = true;
        this.label10.Location = new System.Drawing.Point(199, 14);
        this.label10.Name = "label10";
        this.label10.Size = new System.Drawing.Size(65, 12);
        this.label10.TabIndex = 1311;
        this.label10.Text = "属性名称:";
        // 
        // textBox11
        // 
        this.textBox11.Location = new System.Drawing.Point(90, 70);
        this.textBox11.Name = "textBox11";
        this.textBox11.Size = new System.Drawing.Size(100, 21);
        this.textBox11.TabIndex = 10;
        // 
        // textBox10
        // 
        this.textBox10.Location = new System.Drawing.Point(270, 11);
        this.textBox10.Name = "textBox10";
        this.textBox10.Size = new System.Drawing.Size(100, 21);
        this.textBox10.TabIndex = 7;
        // 
        // textBox9
        // 
        this.textBox9.Location = new System.Drawing.Point(90, 42);
        this.textBox9.Name = "textBox9";
        this.textBox9.Size = new System.Drawing.Size(100, 21);
        this.textBox9.TabIndex = 9;
        // 
        // textBox8
        // 
        this.textBox8.Location = new System.Drawing.Point(270, 42);
        this.textBox8.Name = "textBox8";
        this.textBox8.Size = new System.Drawing.Size(100, 21);
        this.textBox8.TabIndex = 8;
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(765, 419);
        this.Controls.Add(this.groupBox5);
        this.Controls.Add(this.groupBox6);
        this.Controls.Add(this.groupBox2);
        this.Controls.Add(this.groupBox1);
        this.Controls.Add(this.groupBox4);
        this.Name = "Form1";
        this.Text = "用C#操作XML文件";
        this.Load += new System.EventHandler(this.Form1_Load);
        this.groupBox5.ResumeLayout(false);
        ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
        this.groupBox6.ResumeLayout(false);
        this.groupBox6.PerformLayout();
        this.groupBox2.ResumeLayout(false);
        this.groupBox1.ResumeLayout(false);
        this.groupBox1.PerformLayout();
        this.groupBox4.ResumeLayout(false);
        this.groupBox4.PerformLayout();
        this.ResumeLayout(false);

    }

    #endregion

    private System.Windows.Forms.GroupBox groupBox5;
    private System.Windows.Forms.DataGridView dataGridView1;
    private System.Windows.Forms.Label label18;
    private System.Windows.Forms.TextBox root;
    private System.Windows.Forms.TextBox textBox4;
    private System.Windows.Forms.TextBox firstNode;
    private System.Windows.Forms.TextBox childNode;
    private System.Windows.Forms.TextBox attribute;
    private System.Windows.Forms.GroupBox groupBox6;
    private System.Windows.Forms.Label label20;
    private System.Windows.Forms.TextBox nodeContent;
    private System.Windows.Forms.Label label17;
    private System.Windows.Forms.Label label19;
    private System.Windows.Forms.Label label16;
    private System.Windows.Forms.Label label15;
    private System.Windows.Forms.GroupBox groupBox2;
    private System.Windows.Forms.Button button2;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Button button7;
    private System.Windows.Forms.Label label5;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.GroupBox groupBox1;
    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.TextBox textBox6;
    private System.Windows.Forms.TextBox textBox2;
    private System.Windows.Forms.TextBox textBox3;
    private System.Windows.Forms.Label label4;
    private System.Windows.Forms.TextBox textBox5;
    private System.Windows.Forms.Label label3;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Label label9;
    private System.Windows.Forms.Label label8;
    private System.Windows.Forms.GroupBox groupBox4;
    private System.Windows.Forms.TextBox textBox7;
    private System.Windows.Forms.Button button3;
    private System.Windows.Forms.Label label23;
    private System.Windows.Forms.Label label22;
    private System.Windows.Forms.Label label21;
    private System.Windows.Forms.Label label10;
    private System.Windows.Forms.TextBox textBox11;
    private System.Windows.Forms.TextBox textBox10;
    private System.Windows.Forms.TextBox textBox9;
    private System.Windows.Forms.TextBox textBox8;
}

到此这篇关于C#实现XML文件操作详解的文章就介绍到这了,更多相关C# XML文件操作内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: C#实现XML文件操作详解

本文链接: https://www.lsjlt.com/news/175616.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • C#实现XML文件操作详解
    目录实践过程效果代码实践过程 效果 代码 public partial class Form1 : Form { public Form1() { ...
    99+
    2022-12-23
    C# XML文件操作 C# XML 操作 C# XML
  • C#实现利用Linq操作Xml文件
    目录实践过程效果代码实践过程 效果 代码 public partial class Form1 : Form { public Form1() { ...
    99+
    2022-12-29
    C# Linq操作Xml C# 操作Xml C# Linq Xml
  • C#实用教程操作xml文件
    操作XML文件是C#编程中非常常见的任务之一。下面是一个简单的C#实用教程,演示如何使用C#读取、编辑和保存XML文件。读取XML文...
    99+
    2023-09-15
    C#
  • python处理xml文件操作详解
    目录1、python 操作xml的方式介绍2、ElementTree模块3、解析xml格式字符串并获取根节点4、读取节点内容,getroot()5、通标标签名直接获取标签(find,...
    99+
    2022-11-11
  • C#操作XML方法详解
    目录 using System.Xml; //初始化一个xml实例 XmlDocument xml=new XmlDocument(); //导入指定xml文件 xml.Load(...
    99+
    2022-11-12
  • C#操作xml文件的方法
    本篇内容介绍了“C#操作xml文件的方法”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!C#操作xml文件实例是如何的呢?让我们先看看问题:已...
    99+
    2023-06-17
  • C# XmlDocument操作XML案例详解
    C# XmlDocument操作XML XML:Extensible Markup Language(可扩展标记语言)的缩写,是用来定义其它语言的一种元语言,其前身是SGML(Sta...
    99+
    2022-11-12
  • C语言文件操作详解
    目录一、什么是文件二、文件缓冲区三、文件指针四、文件的打开和关闭。总结一、什么是文件 在程序设计中,我们一般谈的文件有两种:程序文件、数据文件。 程序文件: 包括源程序文件(后缀为....
    99+
    2022-11-12
  • C++文件读写操作详解
    目录一、读写文本文件1.1 写文件1.2读文件二、读写二进制文件2.1 写文件2.2 读文件一、读写文本文件 1.1 写文件 写文件步骤如下: 包含头文件 #include <...
    99+
    2022-11-13
  • C#下使用XmlDocument操作XML详解
    目录一、XML DOM概述二、XML成员1、XMl节点:XmlNode1、属性:2、方法:2、XML文档:XMLDocument1、属性:2、方法:3、事件:3、XML元素:XmlE...
    99+
    2022-11-13
  • C++文件流读写操作详解
    目录1.打开文件1.1 fstream类型1.2 open()的函数原型1.3 打开方式1.4 打开文件的属性1.5 示例代码2.文本文件的读写2.1 写文件示例2.2 读文件示例2...
    99+
    2022-11-12
  • C++中文件操作基础详解
    目录文件操作文件分类文本文件写文件读文件二进制文件写文件读文件文件操作 意义:利用文件操作可以保存我们程序运行的信息,是持久化技术的体现 文件分类 按文件类型分为: 1、文本文件 &...
    99+
    2022-11-13
  • C#操作XML文件要注意什么
    本篇内容主要讲解“C#操作XML文件要注意什么”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“C#操作XML文件要注意什么”吧!要对XML文件进行操作,须要声明以下命名空间:using Syste...
    99+
    2023-06-17
  • 详解C#操作XML的方法总结
    本文的主要模块为: 1.生成xml文件 2.遍历xml文件的节点信息 3.修改xml文件的节点信息 4.向xml文件添加节点信息 5.删除指定xml文件的节点信息 假设我们需要设计出...
    99+
    2022-11-13
    C#操作XML方法 C#操作XML C# XML
  • 详解C语言之文件操作(上)
    目录什么是文件程序文件数据文件文件名文件类型文件缓冲区文件指针 文件的打开和关闭输入和输出总结什么是文件 磁盘上的文件就是文件。 在程序设计中,我们一般谈的文件有两种:程序...
    99+
    2022-11-12
  • 详解C语言之文件操作下)
    目录文件的随机读写fseek函数ftell函数rewind函数文件结束判定feof函数和ferror函数总结文件的随机读写 之前的函数只能实现顺序读写,而实现随机读写需用fseek函...
    99+
    2022-11-12
  • C语言中的文件操作详解
    目录1.为什么使用文件2.什么是文件2.1程序文件2.2数据文件2.3文件名3.文件的打开和关闭3.1文件指针3.2文件的打开和关闭4.文件的顺序读写5.文件的随机读写5.1fsee...
    99+
    2022-11-13
  • C#操作INI文件的方法详解
    目录INI文件介绍kernel32Demo案例实现功能程序代码扩展作用本文主要介绍通过调用kernel32函数,实现对ini文件的读取和写入。 INI文件介绍 INI文件全称是Ini...
    99+
    2022-11-13
    C#操作INI文件 C#操作INI C# INI文件
  • C语言实现对文件进行操作的示例详解
    目录前言文件指针文件的打开和关闭文件的打开方式文件读写函数二进制方式存储文件对文件进行拷贝文件随机读写函数文件缓冲区前言 文件操作 在运行程序的时候,此时数据是存放在内存中,当程序退...
    99+
    2023-05-15
    C语言实现文件操作 C语言文件操作 C语言文件
  • C++中TinyXML读取xml文件用法详解
    目录前言XML文件理解常用的XML类方法使用总结前言 TinyXML下载地址:https://sourceforge.net/projects/tinyxml/ 官方文档:TinyX...
    99+
    2022-11-13
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作