C# 讀寫XML檔案例項程式碼
阿新 • • 發佈:2020-03-05
C#史上最簡單讀寫xml檔案方式,建立控制檯應用程式賦值程式碼,就可以執行,需要改動,請自行調整
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>>(); 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; } } }
內容擴充套件:
例項程式碼
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內容事件 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
檔案很大的情況下,可以考慮手動實現資料更新介面卡,比如手動實現一個xml節點搜尋/更新,這樣就不用重寫整個xml。
如果程式的i/o不是主要問題,還是用實體類整個的寫入更新吧,畢竟資料的完整性是第一位的。
如是文章類的,對該目錄建一個xml索引檔案來存放文章的編號,url等,用xml的attribute作為標記不同欄位,內容頁面可以用另外的html或xml頁面存放,用linq to xml操作資料,效率不是很差,個人觀點。當搜尋時候只要查詢指定檔名xml或檔案型別就可以了。
到此這篇關於C# 讀寫XML檔案例項程式碼的文章就介紹到這了,更多相關C# 讀寫XML檔案最簡單方法內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!