C#实现XML文件操作详解

这篇文章主要为大家详细介绍了如何利用C#实现XML操作文件,文中的示例代码讲解详细,对我们学习C#有一定的帮助,感兴趣的小伙伴可以跟随小编一起了解一下

实践过程

效果

代码

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  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 
partial class Form1 { ///  /// 必需的设计器变量。 ///  private System.ComponentModel.IContainer components = null; ///  /// 清理所有正在使用的资源。 ///  /// 如果应释放托管资源,为 true;否则为 false。 protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗体设计器生成的代码 ///  /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 ///  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文件操作内容请搜索0133技术站以前的文章或继续浏览下面的相关文章希望大家以后多多支持0133技术站!

以上就是C#实现XML文件操作详解的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » 其他教程