C# winfrom实现读取修改xml

时间:2021-05-19

本文示例为大家分享了winfrom实现读取修改xml的具体代码,供大家参考,具体内容如下

在winfrom窗体中放一个文本框,2个按钮,一个panle,如下图


form.cs文件中的代码:

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;namespace XMLConfiger{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } public string Path; xmlConfig xmlconfig; //读取xml内容 private void button1_Click(object sender, EventArgs e) { OpenFileDialog fileName = new OpenFileDialog();//定义一个文件打开控件 fileName.InitialDirectory = Application.StartupPath;//设置打开控件后,默认目录为exe运行文件所在文件夹 fileName.Filter = "所有XML文件|*.XML";//设置控件打开的文件类型 fileName.FilterIndex = 2;//设置控件打开文件类型的显示顺序 fileName.RestoreDirectory = true;//设置对话框是否记忆之前打开的目录 if (fileName.ShowDialog() == DialogResult.OK) { Path = fileName.FileName.ToString();//获得用户选择的完整路径 Name = Path.Substring(Path.LastIndexOf("\\") + 1);//获取用户选择的不带路径的文件名 xmlconfig = new xmlConfig(Path); int count = xmlconfig.GetCount(); int ysplit = 30; int x1 = 3; for (int i = 0; i < count; i++) { Label lb = new Label(); lb.Text = xmlconfig.GetName(i).ToString(); lb.Tag = ""; lb.Size = new System.Drawing.Size(60, 23); lb.AutoSize = false; TextBox tb = new TextBox(); tb.Text = xmlconfig.GetXmlNode(i).ToString(); tb.Tag = i; lb.Location = new Point(x1, i * ysplit); tb.Location = new Point(x1 + lb.Size.Width + 10, i * ysplit); panel1.Controls.Add(lb); panel1.Controls.Add(tb); } } } //修改xml内容 private void button2_Click(object sender, EventArgs e) { for (int i = 0; i < this.panel1.Controls.Count; i++) { if (this.panel1.Controls[i].Tag != null && this.panel1.Controls[i].Tag.ToString() != "") { TextBox textbox1 = (TextBox)(this.panel1.Controls[i]); xmlconfig.SavaXMLConfig(Convert.ToInt32(textbox1.Tag), textbox1.Text); } } xmlconfig.SavaConfig(); } }}

xmlConfig.cs中的代码:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Xml;using System.IO;using System.Data;using System.Windows.Forms;namespace XMLConfiger{ public class xmlConfig { public int count = 0; public string path=""; private List<string> strlist = new List<string>(); private List<string> listName = new List<string>(); //构造函数获得所有信息 public xmlConfig(string Path) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(Path);//读取指定的XML文档 path = Path; XmlNode roomlist = xmlDoc.SelectSingleNode("rss"); XmlNodeList list = roomlist.ChildNodes; foreach (XmlNode item in list) { listName.Add(item.Attributes["Name"].Value); strlist.Add(item.InnerText); count = listName.Count; } } //获取所有节点的个数 public int GetCount() { return count; } //通过tag值获取当前返回的Name public string GetName(int tag) { return listName[tag]; } //通过tag值获取当前返回的value public string GetXmlNode(int tag) { return strlist[tag]; } //修改xml中所有的内容 public void SavaConfig() { XmlDocument XMLDoc = new XmlDocument(); XMLDoc.Load(path); XmlNodeList nodeList=XMLDoc.SelectSingleNode("rss").ChildNodes;//获取节点的所有子节点 for (int i = 0; i < nodeList.Count; i++)//遍历所有子节点 { XmlElement xe = (XmlElement)nodeList[i]; XmlNode ChildXml = nodeList[i]; for (int j = 0; j < strlist.Count; j++) { if (listName[j] == ChildXml.Attributes["Name"].Value) { xe.SetAttribute("Name", listName[i]); xe.InnerText = strlist[i]; break; } } } XMLDoc.Save(path);//保存。 } //修改xml中某一个节点 public void SavaXMLConfig(int tag, string Name) { strlist[tag] = Name; } }}

xml文件:

<?xml version="1.0" encoding="utf-8"?><rss version="2.0"> <Student Name="姓名">宁泽涛</Student> <Age Name="年龄">22</Age> <Hobby Name="爱好">游泳</Hobby></rss>

以上就是本文的全部内容,希望对大家的学习有所帮助。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章