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

C# 讀寫XML文件實(shí)例代碼

 更新時間:2020年03月04日 10:21:20   作者:OmySql  
在本篇文章里小編給大家整理的是關(guān)于C# 讀寫XML文件最簡單方法,需要的朋友們可以跟著學(xué)習(xí)參考下。

C#史上最簡單讀寫xml文件方式,創(chuàng)建控制臺應(yīng)用程序賦值代碼,就可以運(yùn)行,需要改動,請自行調(diào)整

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace ConsoleApp1
{
  class Program
  {
    public const String xmlPath = "info.xml";

    static void Main(string[] args)
    {

      IDictionary<String, List<String>> infos = new Dictionary<String, List<String>>();

      infos.Add("Evan", new List<string>() { "123", "456" });

      SaveXML(infos);

      ReadXML();
      Console.ReadKey();
    }

    public static void SaveXML(IDictionary<String, List<String>> infos)
    {
      if (infos == null || infos.Count == 0)
      {
        return;
      }

      XmlDocument xmlDoc = new XmlDocument();

      XmlDeclaration dec = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);

      xmlDoc.AppendChild(dec);

      XmlElement _infos = xmlDoc.CreateElement("infos");

      foreach (KeyValuePair<String, List<String>> item in infos)
      {
        XmlElement info = xmlDoc.CreateElement("info");

        XmlElement name = xmlDoc.CreateElement("file1");
        name.InnerText = item.Key;

        info.AppendChild(name);

        XmlNode filelist = xmlDoc.CreateElement("filelist");

        info.AppendChild(filelist);

        foreach (String number in item.Value)
        {
          XmlElement filed = xmlDoc.CreateElement("filed");
          filed.InnerText = number;

          filelist.AppendChild(filed);
        }

        _infos.AppendChild(info);
      }

      xmlDoc.AppendChild(_infos);

      xmlDoc.Save(xmlPath);
    }

    public static IDictionary<String, List<String>> ReadXML()
    {
      IDictionary<String, List<String>> infos = new Dictionary<String, List<String>>();

      if (File.Exists(xmlPath))
      {
        XmlDocument xmlDoc = new XmlDocument();

        xmlDoc.Load(xmlPath);

        XmlNode xn = xmlDoc.SelectSingleNode("infos");

        XmlNodeList xnl = xn.ChildNodes;

        foreach (XmlNode xnf in xnl)
        {
          XmlElement xe = (XmlElement)xnf;

          XmlNode nameNode = xe.SelectSingleNode("file1");

          string name = nameNode.InnerText;
          Console.WriteLine(name);
          XmlNode filelist = xe.SelectSingleNode("filelist");

          List<String> list = new List<string>();

          foreach (XmlNode item in filelist.ChildNodes)
          {
            list.Add(item.InnerText);
          }

          infos.Add(name, list);
        }
      }

      return infos;
    }
  }
}

內(nèi)容擴(kuò)展:

實(shí)例代碼

dim domxmldocument as system.xml.xmldocument 
  dim tmppath as string = apptempfilepath 
  dim xmlfile as string = tmppath + "\testxml.xml" 
 '窗體加載事件 
  private sub testxml_load(byval sender as system.object, byval e as system.eventargs) handles mybase.load 
  '讀xml過程測試通過 
  dim domxmldocument as system.xml.xmldocument 
  dim tmppath as string = apptempfilepath 
  dim xmlfile as string = tmppath + "\testxml.xml" 
  dim reader as system.xml.xmlreader = nothing 
  try 
  reader = new xml.xmltextreader(xmlfile) 
  'reader. 
  while reader.read 
  me.lboxxml.items.add(reader.name + reader.value) 
  end while 
  catch ex as exception 
  msgbox(ex.message) 
  finally 
  if not (reader is nothing) then 
  reader.close() 
  end if 
  end try 
  end sub 
  '載入xml事件 
  private sub btnxmlload_click(byval sender as system.object, byval e as system.eventargs) handles btnxmlload.click 
  'me.lboxxml.items.clear() 
  ''讀xml過程測試通過 
  'dim reader as system.xml.xmlreader = nothing 
  'try 
  ' reader = new xml.xmltextreader(xmlfile) 
  ' while reader.read 
  ' me.lboxxml.items.add(reader.name + ":" + reader.value) 
  ' end while 
  'catch ex as exception 
  ' msgbox(ex.message) 
  'finally 
  ' if not (reader is nothing) then 
  ' reader.close() 
  ' end if 
  'end try 
  dim ds as new dataset 
  try 
  '如果直接使用ds做datasource則不會展開datagrid,用dv則能直接顯示正確。 
  ds.readxml(xmlfile) 
  dim tb as datatable 
  dim dv as dataview 
  tb = ds.tables(0) 
  dv = new dataview(tb) 
  datagrid1.datasource = dv 
  'datagrid1.datamember = "testxmlmember" 
  'datagrid1.datamember = "employeefname" 
  'dim dxd as new xmldatadocument 
  catch ex as exception 
  msgbox(ex.message.tostring) 
  end try 
  end sub 
  '保存新建xml內(nèi)容事件 
  private sub btnsavenew_click(byval sender as system.object, byval e as system.eventargs) handles btnsavenew.click 
  dim mytw as new xmltextwriter(tmppath + "\testxmlwrite.xml", nothing) 
  mytw.writestartdocument() 
  mytw.formatting = formatting.indented 
  mytw.writestartelement("team") 
  mytw.writestartelement("player") 
  mytw.writeattributestring("name", "george zip") 
  mytw.writeattributestring("position", "qb") 
  mytw.writeelementstring("nickname", "zippy") 
  mytw.writeelementstring("jerseynumber", xmlconvert.tostring(7)) 
  mytw.writeendelement() 
  mytw.writeendelement() 
  mytw.writeenddocument() 
  mytw.close() 
  end sub

文件很大的情況下,可以考慮手動實(shí)現(xiàn)數(shù)據(jù)更新適配器,比如手動實(shí)現(xiàn)一個xml節(jié)點(diǎn)搜索/更新,這樣就不用重寫整個xml。
如果程序的i/o不是主要問題,還是用實(shí)體類整個的寫入更新吧,畢竟數(shù)據(jù)的完整性是第一位的。
如是文章類的,對該目錄建一個xml索引文件來存放文章的編號,url等,用xml的attribute作為標(biāo)記不同字段,內(nèi)容頁面可以用另外的html或xml頁面存放,用linq to xml操作數(shù)據(jù),效率不是很差,個人觀點(diǎn)。當(dāng)搜索時候只要查詢指定文件名xml或文件類型就可以了。

到此這篇關(guān)于C# 讀寫XML文件實(shí)例代碼的文章就介紹到這了,更多相關(guān)C# 讀寫XML文件最簡單方法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C# 減少嵌套循環(huán)的兩種方法

    C# 減少嵌套循環(huán)的兩種方法

    最近在解決性能優(yōu)化的問題,看到了一堆嵌套循環(huán),四五層級的循環(huán)真的有點(diǎn)過分了,在數(shù)據(jù)量成萬,十萬級別的時候,真的非常影響性能。本文介紹了C# 減少嵌套循環(huán)的兩種方法,幫助各位選擇適合自己的優(yōu)化方案,優(yōu)化程序性能
    2021-06-06
  • C# yield關(guān)鍵字詳解

    C# yield關(guān)鍵字詳解

    這篇文章主要介紹了C# yield關(guān)鍵字詳解,本文講解了yield是一個語法糖、語法糖的實(shí)現(xiàn)(實(shí)現(xiàn)IEnumerable<T>接口的類)、yield使用中的特殊情況等內(nèi)容,需要的朋友可以參考下
    2015-04-04
  • C#利用緩存分塊讀寫大文件

    C#利用緩存分塊讀寫大文件

    這篇文章主要為大家詳細(xì)介紹了C#利用緩存分塊讀寫大文件,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • C#中l(wèi)ist用法實(shí)例

    C#中l(wèi)ist用法實(shí)例

    這篇文章主要介紹了C#中l(wèi)ist用法,結(jié)合實(shí)例形式分析了C#中l(wèi)ist排序、運(yùn)算、轉(zhuǎn)換等常見操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2016-06-06
  • Unity3D實(shí)現(xiàn)甜品消消樂游戲

    Unity3D實(shí)現(xiàn)甜品消消樂游戲

    這篇文章主要介紹了通過C# Unity3D繪制一個甜品消消樂游戲,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)或工作有一定的幫助,感興趣的小伙伴可以學(xué)習(xí)一下
    2021-12-12
  • C#利用FileSystemWatcher實(shí)時監(jiān)控文件的增加,修改,重命名和刪除

    C#利用FileSystemWatcher實(shí)時監(jiān)控文件的增加,修改,重命名和刪除

    好多時候,我們都需要知道某些目錄下的文件什么時候被修改、刪除過等。本文將利用FileSystemWatcher實(shí)現(xiàn)實(shí)時監(jiān)控文件的增加,修改,重命名和刪除,感興趣的可以了解一下
    2022-08-08
  • C#使用Process類調(diào)用外部程序分解

    C#使用Process類調(diào)用外部程序分解

    這篇文章主要介紹了C#使用Process類調(diào)用外部程序分解,分別介紹了啟動外部程序、關(guān)掉外部程序、關(guān)掉后調(diào)用一些方法的方法,需要的朋友可以參考下
    2014-07-07
  • c#執(zhí)行excel宏模版的方法

    c#執(zhí)行excel宏模版的方法

    這篇文章主要介紹了c#執(zhí)行excel宏模版的方法,導(dǎo)出Excel模版的代碼如下,大家參考使用吧
    2014-01-01
  • C#中Random.Next方法的使用小結(jié)

    C#中Random.Next方法的使用小結(jié)

    在C#中,Random.Next()方法用于生成一個隨機(jī)整數(shù),本文主要介紹了C#中Random.Next方法的使用小結(jié),具有一定的參考價值,感興趣的可以了解一下
    2024-01-01
  • 淺析C# 索引器(Indexer)

    淺析C# 索引器(Indexer)

    這篇文章主要介紹了C# 索引器(Indexer)的相關(guān)資料,文中示例代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07

最新評論