C# winfrom完成讀取修正xml。本站提示廣大學習愛好者:(C# winfrom完成讀取修正xml)文章只能為提供參考,不一定能成為您想要的結果。以下是C# 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>
以上就是本文的全體內容,願望對年夜家的進修有所贊助。