winfrom 实现读取修改xml

前端之家收集整理的这篇文章主要介绍了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>

猜你在找的XML相关文章