C#史上最簡(jiǎn)單讀寫(xiě)xml文件方式,創(chuàng)建控制臺(tái)應(yīng)用程序賦值代碼,就可以運(yùn)行,需要改動(dòng),請(qǐng)自行調(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> infos = new Dictionary >(); infos.Add("Evan", new List () { "123", "456" }); SaveXML(infos); ReadXML(); Console.ReadKey(); } public static void SaveXML(IDictionary > 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 > 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 > ReadXML() { IDictionary > infos = new Dictionary >(); 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 list = new List (); foreach (XmlNode item in filelist.ChildNodes) { list.Add(item.InnerText); } infos.Add(name, list); } } return infos; } } }
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線(xiàn),公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性?xún)r(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿(mǎn)足用戶(hù)豐富、多元化的應(yīng)用場(chǎng)景需求。