欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C# winfrom實現(xiàn)讀取修改xml

 更新時間:2016年05月11日 16:03:07   作者:wangjingjing1014  
這篇文章主要為大家詳細介紹了C# winfrom實現(xiàn)讀取修改xml的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下

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

在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內(nèi)容
  private void button1_Click(object sender, EventArgs e)
  {


   OpenFileDialog fileName = new OpenFileDialog();//定義一個文件打開控件
   fileName.InitialDirectory = Application.StartupPath;//設(shè)置打開控件后,默認目錄為exe運行文件所在文件夾
   fileName.Filter = "所有XML文件|*.XML";//設(shè)置控件打開的文件類型
   fileName.FilterIndex = 2;//設(shè)置控件打開文件類型的顯示順序
   fileName.RestoreDirectory = true;//設(shè)置對話框是否記憶之前打開的目錄
   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內(nèi)容
  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>();
  //構(gòu)造函數(shù)獲得所有信息
  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;
   }
  
  }
  //獲取所有節(jié)點的個數(shù)
  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中所有的內(nèi)容
  public void SavaConfig()
  {
   XmlDocument XMLDoc = new XmlDocument();
   XMLDoc.Load(path);
   XmlNodeList nodeList=XMLDoc.SelectSingleNode("rss").ChildNodes;//獲取節(jié)點的所有子節(jié)點 
   for (int i = 0; i < nodeList.Count; i++)//遍歷所有子節(jié)點 
   {
    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中某一個節(jié)點
  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>

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

相關(guān)文章

  • C#獲取文件相關(guān)信息的方法

    C#獲取文件相關(guān)信息的方法

    這篇文章主要介紹了C#獲取文件相關(guān)信息的方法,涉及C#中FileInfo類的相關(guān)使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-04-04
  • Unity搖桿制作的方法

    Unity搖桿制作的方法

    這篇文章主要為大家詳細介紹了Unity搖桿制作的方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • 手把手教你如何基于C#制作一個網(wǎng)址檢測工具

    手把手教你如何基于C#制作一個網(wǎng)址檢測工具

    這篇文章主要給大家介紹了關(guān)于如何基于C#制作一個網(wǎng)址檢測工具的相關(guān)資料,文中通過圖文以及實例代碼介紹的非常詳細,對大家學習或者使用C#具有一定的參考學習價值,需要的朋友可以參考下
    2023-02-02
  • C#實現(xiàn)矩陣乘法實例分析

    C#實現(xiàn)矩陣乘法實例分析

    這篇文章主要介紹了C#實現(xiàn)矩陣乘法的方法,實例分析了通過C#數(shù)組構(gòu)造矩陣及實現(xiàn)矩陣乘法的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-04-04
  • 理解C#中的事件

    理解C#中的事件

    這篇文章主要介紹了理解C#中的事件,本文講解了使用委托中的問題、事件的出現(xiàn)、深入理解事件、C#屬性的概念、事件代碼的轉(zhuǎn)換等內(nèi)容,需要的朋友可以參考下
    2015-02-02
  • C#使用yield關(guān)鍵字構(gòu)建迭代器詳解

    C#使用yield關(guān)鍵字構(gòu)建迭代器詳解

    這篇文章主要為大家詳細介紹了C#使用yield關(guān)鍵字構(gòu)建迭代器的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • C#中常用窗口特效的實現(xiàn)代碼

    C#中常用窗口特效的實現(xiàn)代碼

    這篇文章主要為大家詳細介紹了C#中三個常用的窗口特效的實現(xiàn),分別是淡入淡出、變大變小、緩升緩降,感興趣的小伙伴可以跟隨小編一起學習一下
    2023-12-12
  • C#?WPF實現(xiàn)3D操作幾何體效果

    C#?WPF實現(xiàn)3D操作幾何體效果

    眾所周知,我的世界就是無數(shù)個像素塊的集合,而像素塊也就是立方體。關(guān)于新建立方體,這個大家已經(jīng)非常熟練了,本文就來說說如何實現(xiàn)3D操作幾何體效果吧
    2023-03-03
  • C#手動操作DataGridView使用各種數(shù)據(jù)源填充表格實例

    C#手動操作DataGridView使用各種數(shù)據(jù)源填充表格實例

    本文主要介紹了C#手動操作DataGridView使用各種數(shù)據(jù)源填充表格實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-02-02
  • C#使用RSA加密解密文件

    C#使用RSA加密解密文件

    這篇文章主要為大家詳細介紹了C#使用RSA加密解密文件,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-03-03

最新評論