加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

winfrom 实现读取修改xml

发布时间:2020-12-16 06:11:19 所属栏目:百科 来源:网络整理
导读:在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 Sy

在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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读