本文示例為大家分享了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>
以上就是本文的全部內容,希望對大家的學習有所幫助。