开发者

C#实现利用Linq操作Xml文件

目录
  • 实践过程
    • 效果
    • 代码

实践过程

效果

C#实现利用Linq操作Xml文件

代码

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

    static string strPath = "Employee.XML";
    static string strID = "";

    //窗体加载时加载XML文件
    private void Form1_Load(object sender, EventArgs e)
    {
        if (File.Exists(strPath))
        {
            groupBox1.Enabled = false;
            getXmlInfo();
        }
        else
            groupBox1.Enabled = true;
    }

    //创建XML文件
    private void button1_Click(object sender, EventArgs e)
    {
        XDocument doc = new XDocument(
            new XDeclaration("1.0", "utf-8", "yes"),
            new XElement(textBox1.Text,
                new XElement(textBox2.Text, new XAttribute(textBox3.Text, textBox10.Text),
                    new XElement(textBox4.Text, textBox5.Text),
                    new XElement(textBox6.Text, textBox7.Text),
                    new XElement(textBox8.Text, textBox9.Text))
                )
            );
        doc.Save(strPath);
        groupBox1.Enabled = false;
        getXmlInfo();
    }

    //添加XML元素
    private void button2_Click(object sender, EventArgs e)
    {
        XElement xe = XElement.Load(strPath);
        IEnumerable<XElement> elements1 = from element in xe.Elements("People")
                                          select element;
        //生成新的编号
        string str = (Convert.ToInt32(elements1.Max(element => element.Attribute("ID").Value)) + 1).ToString("000");
        XElement people = new XElement(
            "People", new XAttribute("ID", str),
            new XElement("Name", textBox11.Text),
            new XElement("Sex", comboBox1.Text),
            new XElement("Salary", textBox12.Text)
            );
        xe.Add(people);
        xe.Save(strPath);
        getXmlInfo();
    }

    //修改XML元素
    private void button3_Click(object sender, EventArgs e)
    {
        if (strID != "")
        {
            XElement xe = XElement.Load(strPath);
            IEnumerable<XElement> elements = from element in xe.Elements("People")
                                             where element.Attribute("ID").Value == strID
                                             select element;
            if (elements.Count() > 0)
            {
                XElement newXE = elements.First();
                newXE.SetAttributeValue("ID", strID);
                newXE.ReplaceNodes(
                    new XElement("Name", textBox11.Text),
                    new XElement("Sex", comboBox1.Text),
                    new XElement("Salary", textBox12.Text)
                    );
            }
            xe.Save(strPath);
        } 
        getXmlInfo();
    }

    //删除XML元素
    private void button4_Click(object sender, EventArgs e)
    {
        if (strID != "")
        {
            XElement xe = XElement.Load(strPath);
            IEnumerable<XElement> elements = from element in xe.Elements("People")
                                             where element.Attribute("ID").Value == strID
                                             select element;
            if (elements.Count() > 0)
                elements.First().Remove();
            xe.Save(strPath);
        }
        getXmlInfo();
    }

    //显示选中XML节点的详细信息
    private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        strID = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();
        XElement xe = XElement.Load(strPath);
        IEnumerable<XElement> elements = from PInfo in xe.Elements("People")
                                         where PInfo.Attribute("ID").Value == strID
                                         select PInfo;
        foreach (XElement element in elements)
        {
            textBox11.Text = element.Element("Name").Value;
            comboBox1.SelectedItem = element.Element("Sex").Value;
            textBox12.Text = element.Element("Salary").Value;
        }
    }

    #region 将XML文件内容绑定到DataGridView控件
    /// <summary>
    /// 将XML文件内容绑定到DataGridView控件
    /// </summary>
    private void getXmlInfo()
    {
        DataSet myds = new DataSet();
        myds.ReadXml(strPath);
        dataGridView1.DataSource = myds.Tables[0];
    }
    #endregion
}
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.groupBox1 = new System.Windows.Forms.GroupBox();
        this.textBox10 = new System.Windows.Forms.TextBox();
        this.label10 = new System.Windows.Forms.Label();
        this.button1 = new System.Windows.Forms.Button();
        this.textBox9 = new System.Windows.Forms.TextBox();
        this.label8 = new System.Windows.Forms.Label();
        this.textBox8 = new System.Windows.Forms.TextBox();
        this.label9 = new System.Windows.Forms.Label();
        this.textBox7 = new System.Windows.Forms.TextBox();
        this.label2 = new System.Windows.Forms.Label();
        this.textBox6 = new System.Windows.Forms.TextBox();
        this.label7 = new System.Windows.Forms.Label();
        this.textBox5 = new System.Windows.Forms.TextBox();
        this.label5 = new System.Windows.Forms.Label();
        this.textBox4 = new System.Windows.Forms.TextBox();
        this.label6 = new System.Windows.Forms.Label();
        this.textBox3 = new System.Windows.Forms.TextBox();
        this.label3 = new System.Windows.Forms.Label();
        this.textBox2 = new System.Windows.Forms.TextBox();
        this.label4 = new System.Windows.Forms.Label();
        this.textBox1 = new System.Windows.Forms.TextBox();
        this.label1 = new System.Windows.Forms.Label();
        this.groupBox2 = new System.Windows.Forms.GroupBox();
        this.button4 = new System.Windows.Forms.Button();
        this.button3 = new System.Windows.Forms.Button();
        this.button2 = new System.Windows.Forms.Button();
        this.comboBox1 = new System.Windows.Forms.ComboBox();
        this.textBox12 = new System.Windows.Forms.TextBox();
        this.label12 = new System.Windows.Forms.Label();
        this.label13 = new System.Windows.Forms.Label();
        this.textBox11 = new System.Windows.Forms.TextBox();
        this.label14 = new System.Windows.Forms.Label();
        this.dataGridView1 = new System.Windows.Forms.DataGridView();
        this.groupBox1.SuspendLayout();
        this.groupBox2.SuspendLayout();
        ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
        this.SuspendLayout();
        // 
        // groupBox1
        // 
        this.groupBox1.Controls.Add(this.textBox10);
        this.groupBox1.Controls.Add(this.label10);
        this.groupBox1.Controls.Add(this.button1);
        this.groupBox1.Controls.Add(this.textBox9);
        this.groupBox1.Controls.Add(this.label8);
        this.groupBox1.Controls.Add(this.textBox8);
        this.groupBox1.Controls.Add(this.label9);
        this.groupBox1.Controls.Add(this.textBox7);
        this.groupBox1.Controls.Add(this.label2);
        this.groupBox1.Controls.Add(this.textBox6);
        this.groupBox1.Controls.Add(this.label7);
        this.groupBox1.Controls.Add(this.textBox5);
        this.groupBox1.Controls.Add(this.label5);
        this.groupBox1.Controls.Add(this.textBox4);
        this.groupBox1.Controls.Add(this.label6);
        this.groupBox1.Controls.Add(this.textBox3);
        this.groupBox1.Controls.Add(this.label3);
        this.groupBox1.Controls.Add(this.textBox2);
        this.groupBox1.Controls.Add(this.label4);
        this.groupBox1.Controls.Add(this.textBox1);
        this.groupBox1.Controls.Add(this.label1);
        this.groupBox1.Location = new System.Drawing.Point(6, 3);
        this.groupBox1.Name = "groupBox1";
        this.groupBox1.Size = new System.Drawing.Size(451, 190);
        this.groupBox1.TabIndex = 18;
        this.groupBox1.TabStop = false;
        this.groupBox1.Text = "创建XML文件";
        // 
        // textBox10
        // 
        this.textBox10.Location = new System.Drawing.Point(319, 47);
        this.textBox10.Name = "textBox10";
        this.textBox10.ReadOnly = true;
        this.textBox10.Size = new System.Drawing.Size(119, 21);
        this.textBox10.TabIndex = 3;
        this.textBox10.Text = "001";
        // 
        // label10
        // 
        this.label10.AutoSize = true;
        this.label10.Location = new System.Drawing.Point(236, 50);
        this.label10.Name = "label10";
        this.label10.Size = new System.Drawing.Size(89, 12);
        this.label10.TabIndex = 21;
        this.label10.Text = "子节点属性值:";
        // 
        // button1
        // 
        this.button1.Location = new System.Drawing.Point(363, 158);
        this.button1.Name = "button1";
        this.button1.Size = new System.Drawing.Size(75, 23);
        this.button1.TabIndex = 10;
        this.button1.Text = "创建";
        this.button1.UseVisualStyleBackColor = true;
        this.button1.Click += new System.EventHandler(this.button1_Click);
        // 
        // textBox9
        // 
        this.textBox9.Location = new System.Drawing.Point(319, 131);
        this.textBox9.Name = "textBox9";
        this.textBox9.Size = new System.Drawing.Size(119, 21);
        this.textBox9.TabIndex = 9;
        // 
        // label8
        // 
        this.label8.AutoSize = true;
        this.label8.Location = new System.Drawing.Point(236, 134);
        this.label8.Name = "label8";
        this.label8.Size = new System.Drawing.Size(89, 12);
        this.label8.TabIndex = 18;
        this.label8.Text = "第三个元素值:";
        // 
        // textBox8
        // 
        this.textBox8.Location = new System.Drawing.Point(111, 131);
        this.textBox8.Name = "textBox8";
        this.textBox8.ReadOnly = true;
        this.textBox8.Size = new System.Drawing.Size(119, 21);
        this.textBox8.TabIndex = 8;
        this.textBox8.Text = "Salary";
        // 
        // label9
        // 
        this.label9.AutoSize = true;
        this.label9.Location = new System.Drawing.Point(12, 134);
        this.label9.Name = "label9";
        this.label9.Size = new System.Drawing.Size(101, 12);
        this.label9.TabIndex = 16;
        this.label9.Text = "第三个元素名称:";
        // 
        // textBox7
        // 
        this.textBox7.Location = new System.Drawing.Point(319, 103);
        this.textBox7.Name = "textBox7";
        this.textBox7.Size = new System.Drawing.Size(119, 21);
        this.textBox7.TabIndex = 7;
        // 
        // label2
        // 
        this.label2.AutoSize = true;
        this.label2.Location = new System.Drawing.Point(236, 106);
        this.label2.Name = "label2";
        this.label2.Size = new System.Drawing.Size(89, 12);
        this.lawww.devze.combel2.TabIndex = 14;
        this.label2.Text = "第二个元素值:";
        // 
        // textBox6
        // 
        this.textBox6.Location = new System.Drawing.Point(111, 103);
        this.textBox6.Name = "textBox6";
        this.textBox6.ReadOnly = true;
        this.textBox6.Size = new System.Drawing.Size(119, 21);
        this.textBox6.TabIndex = 6;
        this.textBox6.Text = "Sex";
        // 
        // label7
        // 
        this.label7.AutoSize = true;
        this.label7.Location = new System.Drawing.Point(12, 106);
        this.label7.Name = "label7";
        this.label7.Size = new System.Drawing.Size(101, 12);
        this.label7.TabIndex = 12;
        this.label7.Text = "第二个元素名称:";
        // 
        // textBox5
        // 
        this.textBox5.Location = new System.Drawing.Point(319, 75);
        this.textBox5.Name = "textBox5";
        this.textBox5.Size = new System.Drawing.Size(119, 21);
        this.textBox5.TabIndex = 5;
        // 
        // label5
        // 
        this.label5.AutoSize = true;
        this.label5.Location = new System.Drawing.Point(236, 78);
        this.label5.Name = "label5";
        this.label5.Size = new System.Drawing.Size(89, 12);
        this.label5.TabIndex = 10;
        this.label5.Text = "第一个元素值:";
        // 
        // textBox4
        // 
        this.textBox4.Location = new System.Drawing.Point(111, 75);
  编程客栈      this.textBox4.Name = "textBox4";
        this.textBox4.ReadOnly = true;
        this.textBox4.Size = new System.Drawing.Size(119, 21);
        this.textBox4.TabIndex = 4;
        this.textBox4.Text = "Name";
        // 
        // label6
        // 
        this.label6.AutoSize = true;
        this.label6.Location = new System.Drawing.Point(12, 78);
        this.label6.Name = "label6";
        this.label6.Size = new System.Drawing.Size(101, 12);
        this.label6.TabIndex = 8;
        this.label6.Text = "第一个元素名称:";
        // 
        // textBox3
        // 
        this.textBox3.Location = new System.Drawing.Point(111, 47);
        this.textBox3.Name = "textBox3";
        this.textBox3.ReadOnly = true;
        this.textBox3.Size = new System.Drawing.Size(119, 21);
        this.textBox3.TabIndex = 2;
        this.textBox3.Text = "ID";
        // 
        // label3
        // 
        this.label3.AutoSize = true;
        this.label3.Location = new System.Drawing.Point(28, 50);
        this.label3.Name = "label3";
        this.label3.Size = new System.Drawing.Size(77, 12);
        this.label3.TabIndex = 6;
        this.label3.Text = "子节点属性:";
        // 
        // textBox2
        // 
        this.textBox2.Location = new System.Drawing.Point(319, 20);
        this.textBox2.Name = "textBox2";
        this.textBox2.ReadOnly = true;
        this.textBox2.Size = new System.Drawing.Size(119, 21);
        this.textBox2.TabIndex = 1;
        this.textBox2.Text = "People";
        // 
        // label4
        // 
        this.label4.AutoSize = true;
        this.label4.Location = new System.Drawing.Point(248, 23);
        this.label4.Name = "label4";
        this.label4.Size = new System.Drawing.Size(77, 12);
        this.label4.TabIndex = 4;
        this.label4.Text = "子节点名称:";
        // 
        // textBox1
        // 
        this.textBox1.Location = new System.Drawing.Point(111, 20);
        this.textBox1.Name = "textBox1";
        this.textBox1.ReadOnly = true;
        this.textBox1.Size = new System.Drawing.Size(119, 21);
        this.textBox1.TabIndex = 0;
        this.textBox1.Text = "Peoples";
        // 
        // label1
        // 
        this.label1.AutoSize = true;
        this.label1.Location = new System.Drawing.Point(19, 23);
        this.label1.Nwww.devze.comame = "label1";
        this.label1.Size = new System.Drawing.Size(89, 12);
        this.label1.TabIndex = 0;
        this.label1.Text = "顶级节点名称:";
        // 
        // groupBox2
        // 
        this.groupBox2.Controls.Add(this.button4);
        this.groupBox2.Controls.Add(this.button3);
        this.groupBox2.Controls.Add(this.button2);
        this.groupBox2.Controls.Add(this.comboBox1);
        this.groupBox2.Controls.Add(this.textBox12);
        this.groupBox2.Controls.Add(this.label12);
        this.groupBox2.Controls.Add(this.label13);
        this.groupBox2.Controls.Add(this.textBox11);
        this.groupBox2.Controls.Add(this.label14);
        this.groupBox2.Location = new System.Drawing.Point(6, 198);
        this.groupBox2.Name = "groupBox2";
        this.groupBox2.Size = new System.Drawing.Size(451, 80);
        this.groupBox2.TabIndex = 19;
        this.groupBox2.TabStop = false;
        this.groupBox2.Text = "操作XML文件";
        // 
        // button4
        // 
        this.button4.Location = new System.Drawing.Point(360, 47);
        this.button4.Name = "button4";
        this.button4.Size = new System.Drawing.Size(63, 23);
        this.button4.TabIndex = 16;
        this.button4.Text = "删除";
        this.button4.UseVisualStyleBackColor = true;
        this.button4.Click += new System.EventHandler(this.button4_Click);
        // 
        // button3
        // 
        this.button3.Location = new System.Drawing.Point(291, 47);
        this.button3.Name = "button3";
        this.button3.Size = new System.Drawing.Size(63, 23);
        this.button3.TabIndex = 15;
        this.button3.Text = "修改";
        this.button3.UseVisualStyleBackColor = true;
        this.button3.Click += new System.EventHandler(this.button3_Click);
        // 
        // button2
        // 
        this.button2.Location = new System.Drawing.Point(222, 47);
        this.button2.Name = "button2";
        this.button2.Size = new System.Drawing.Size(63, 23);
        this.button2.TabIndex = 14;
        this.button2.Text = "添加";
        this.button2.UseVisualStyleBackColor = true;
        this.button2.Chttp://www.devze.comlick += new System.EventHandler(this.button2_Click);
        // 
        // comboBox1
        // 
        this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
        this.comboBox1.FormattingEnabled = true;
        this.comboBox1.Items.AddRange(new object[] {
        "男",
        "女"});
        this.comboBox1.Location = new System.Drawing.Point(286, 20);
        this.comboBox1.Name = "comboBox1";
        this.comboBox1.Size = new System.Drawing.Size(121, 20);
        this.comboBox1.TabIndex = 12;
        // 
        // textBox12
        // 
        this.textBox12.Location = new System.Drawing.Point(85, 49);
        this.textBox12.Name = "textBox12";
        this.textBox12.Size = new System.Drawing.Size(119, 21);
        this.textBox12.TabIndex = 13;
        // 
        // label12
        // 
        this.label12.AutoSize = true;
        this.label12.Location = new System.Drawing.Point(19, 52);
        this.label12.Name = "label12";
        this.label12.Size = new System.Drawing.Size(65, 12);
        this.label12.TabIndex = 27;
        this.label12.Text = "职工薪水:";
        // 
        // label13
        // 
        this.label13.AutoSize = true;
        this.label13.Location = new System.Drawing.Point(220, 23);
        this.label13.Name = "label13";
        this.label13.Size = new System.Drawing.Size(65, 12);
        this.label13.TabIndex = 25;
        this.label13.Text = "职工性别:";
        // 
        // textBox11
        // 
        this.textBox11.Location = new System.Drawing.Point(85, 20);
        this.textBox11.Name = "textBox11";
        this.textBox11.Size = new System.Drawing.Size(119, 21);
        this.textBox11.TabIndex = 11;
        // 
        // label14
        // 
        this.label14.AutoSize = true;
        this.label14.Location = new System.Drawing.Point(19, 23);
        this.label14.Name = "label14";
        this.label14.Size = new System.Drawing.Size(65, 12);
        this.label14.TabIndex = 23;
        this.label14.Text = "职工姓名:";
        // 
        // dataGridView1
        // 
        this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
        this.dataGridView1.Location = new System.Drawing.Point(6, 284);
        this.dataGridView1.Name = "dataGridView1";
        this.dataGridView1.RowTemplate.Height = 23;
        this.dataGridView1.Size = new System.Drawing.Size(451, 171);
        this.dataGridView1.TabIndex = 17;
        this.dataGridView1.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellClick);
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.S编程客栈izeF(6F, 12F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(462, 460);
        this.Controls.Add(this.dataGridView1);
        this.Controls.Add(this.groupBox2);
        this.Controls.Add(this.groupBox1);
        this.Name = "Form1";
        this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
        this.Text = "使用Linq To XML操作XML文件";
        this.Load += new System.EventHandler(this.Form1_Load);
        this.groupBox1.ResumeLayout(false);
        this.groupBox1.PerformLayout();
        this.groupBox2.ResumeLayout(false);
        this.groupBox2.PerformLayout();
        ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
        this.ResumeLayout(false);

    }

    #endregion

    private System.Windows.Forms.GroupBox groupBox1;
    private System.Wi开发者_开发培训ndows.Forms.Label label1;
    private System.Windows.Forms.TextBox textBox5;
    private System.Windows.Forms.Label label5;
    private System.Windows.Forms.TextBox textBox4;
    private System.Windows.Forms.Label label6;
    private System.Windows.Forms.TextBox textBox3;
    private System.Windows.Forms.Label label3;
    private System.Windows.Forms.TextBox textBox2;
    private System.Windows.Forms.Label label4;
    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.TextBox textBox7;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.TextBox textBox6;
    private System.Windows.Forms.Label label7;
    private System.Windows.Forms.TextBox textBox9;
    private System.Windows.Forms.Label label8;
    private System.Windows.Forms.TextBox textBox8;
    private System.Windows.Forms.Label label9;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.TextBox textBox10;
    private System.Windows.Forms.Label label10;
    private System.Windows.Forms.GroupBox groupBox2;
    private System.Windows.Forms.DataGridView dataGridView1;
    private System.Windows.Forms.TextBox textBox12;
    private System.Windows.Forms.Label label12;
    private System.Windows.Forms.Label label13;
    private System.Windows.Forms.TextBox textBox11;
    private System.Windows.Forms.Label label14;
    private System.Windows.Forms.ComboBox comboBox1;
    private System.Windows.Forms.Button button4;
    private System.Windows.Forms.Button button3;
    private System.Windows.Forms.Button button2;
}

到此这篇关于C#实现利用Linq操作Xml文件的文章就介绍到这了,更多相关C# Linq操作Xml内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新开发

开发排行榜