欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

C#讀寫XML文件實例代碼

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

創(chuàng)新互聯(lián)是專業(yè)的東河網(wǎng)站建設(shè)公司,東河接單;提供成都網(wǎng)站設(shè)計、網(wǎng)站制作、外貿(mào)營銷網(wǎng)站建設(shè),網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進行東河網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!

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)容擴展:

實例代碼

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

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

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

新聞標題:C#讀寫XML文件實例代碼
網(wǎng)頁鏈接:http://www.chinadenli.net/article22/gpcojc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管網(wǎng)站建設(shè)微信小程序企業(yè)網(wǎng)站制作網(wǎng)站內(nèi)鏈域名注冊

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁設(shè)計公司