欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C#教程

當(dāng)前位置:主頁 > 軟件編程 > C#教程 >

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

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C#教程|點擊: 次

本文示例為大家分享了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è)置打開控件后,默認(rèn)目錄為exe運(yùn)行文件所在文件夾
   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值獲取當(dāng)前返回的Name
  public string GetName(int tag)
  {
   return listName[tag];
  }
  //通過tag值獲取當(dāng)前返回的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)容,希望對大家的學(xué)習(xí)有所幫助。

上一篇:淺析C#中StringBuilder類的高效及與String的對比

欄    目:C#教程

下一篇:C#數(shù)組的常用操作方法小結(jié)

本文標(biāo)題:C# winfrom實現(xiàn)讀取修改xml

本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6512.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有