C# winfrom實現讀取修改xml_C#教程

來源:互聯網
上載者:User

本文樣本為大家分享了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>

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.