国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C# winfrom實現讀取修改xml

2019-10-29 21:28:23
字體:
來源:轉載
供稿:網友
這篇文章主要為大家詳細介紹了C# winfrom實現讀取修改xml的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
 

本文示例為大家分享了winfrom實現讀取修改xml的具體代碼,供大家參考,具體內容如下

在winfrom窗體中放一個文本框,2個按鈕,一個panle,如下圖

C#,winfrom,xml

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>

以上就是本文的全部內容,希望對大家的學習有所幫助。



注:相關教程知識閱讀請移步到c#教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 德兴市| 祁阳县| 大足县| 常德市| 公安县| 仁化县| 磐安县| 延寿县| 邳州市| 和林格尔县| 黔东| 于田县| 新平| 舒兰市| 靖远县| 得荣县| 灵川县| 精河县| 杭锦后旗| 开远市| 盐亭县| 洛浦县| 陕西省| 宜春市| 福州市| 铜梁县| 石城县| 象山县| 鄂尔多斯市| 太原市| 茌平县| 东宁县| 弥勒县| 定南县| 大渡口区| 永济市| 南漳县| 台江县| 景洪市| 遂溪县| 溧阳市|