Linq to Xml 小結(jié)
Linq to xml 這個(gè)東西出來好多年了,但一直沒有機(jī)會(huì)在項(xiàng)目中用到,前段時(shí)間,終于項(xiàng)目中一些地方需要用到xml作為數(shù)據(jù)源,于是就體驗(yàn)了一把這個(gè),感覺還挺不錯(cuò)的,今天在此小結(jié)一下.
首先我們來模擬一下一個(gè)真實(shí)的業(yè)務(wù)場(chǎng)景:在這里我們需要展示一個(gè)火車站上各個(gè)站臺(tái)上面燈光開關(guān)狀態(tài).假設(shè)每個(gè)站臺(tái)一共6個(gè)開關(guān),站臺(tái)我們用Station節(jié)點(diǎn)表示,站臺(tái)開關(guān)我們用SwitchNo來表示,在初始化(即默認(rèn)狀態(tài))的時(shí)候,我們需要表示成第一個(gè)站臺(tái)2個(gè)開關(guān)開著,第二個(gè)站臺(tái)3個(gè)開關(guān)開著,第三個(gè)站臺(tái)4個(gè)開關(guān)開著….
那么我們可能需要的一個(gè)xml文件格式可能就是如下這個(gè)樣子:
<?xml version="1.0" encoding="utf-8"?> <Stations> <Station Id="1"> <SwitchNo Id="1">1</SwitchNo> <SwitchNo Id="2">1</SwitchNo> <SwitchNo Id="3">0</SwitchNo> <SwitchNo Id="4">0</SwitchNo> <SwitchNo Id="5">0</SwitchNo> <SwitchNo Id="6">0</SwitchNo> </Station> <Station Id="2"> <SwitchNo Id="1">1</SwitchNo> <SwitchNo Id="2">1</SwitchNo> <SwitchNo Id="3">1</SwitchNo> <SwitchNo Id="4">0</SwitchNo> <SwitchNo Id="5">0</SwitchNo> <SwitchNo Id="6">0</SwitchNo> </Station> <Station Id="3"> <SwitchNo Id="1">1</SwitchNo> <SwitchNo Id="2">1</SwitchNo> <SwitchNo Id="3">1</SwitchNo> <SwitchNo Id="4">0</SwitchNo> <SwitchNo Id="5">0</SwitchNo> <SwitchNo Id="6">0</SwitchNo> </Station> <Station Id="4"> <SwitchNo Id="1">0</SwitchNo> <SwitchNo Id="2">0</SwitchNo> <SwitchNo Id="3">1</SwitchNo> <SwitchNo Id="4">1</SwitchNo> <SwitchNo Id="5">0</SwitchNo> <SwitchNo Id="6">0</SwitchNo> </Station> <Station Id="5"> <SwitchNo Id="1">1</SwitchNo> <SwitchNo Id="2">1</SwitchNo> <SwitchNo Id="3">1</SwitchNo> <SwitchNo Id="4">1</SwitchNo> <SwitchNo Id="5">1</SwitchNo> <SwitchNo Id="6">0</SwitchNo> </Station> <Station Id="6"> <SwitchNo Id="1">1</SwitchNo> <SwitchNo Id="2">1</SwitchNo> <SwitchNo Id="3">1</SwitchNo> <SwitchNo Id="4">1</SwitchNo> <SwitchNo Id="5">1</SwitchNo> <SwitchNo Id="6">1</SwitchNo> </Station> </Stations>
下面我們就從xml數(shù)據(jù)的創(chuàng)建/查詢/修改這三個(gè)方面簡(jiǎn)單談一下吧.關(guān)于概念啥的就不啰嗦了,直接上代買吧.
1. 創(chuàng)建這個(gè)文件
/// <summary> /// 創(chuàng)建xml文件 /// </summary> private void writeStationXmlData() { string fileName = Path.Combine(dataPath, "StationWitch.xml"); if (!Directory.Exists(dataPath)) Directory.CreateDirectory(dataPath); if (File.Exists(fileName)) return; var stationEleList = new List<XElement>(); //臨時(shí)緩存每個(gè)站臺(tái)節(jié)點(diǎn) //一共6個(gè)站臺(tái),循環(huán)創(chuàng)建 for (int i = 1; i < 7; i++) { var xAttr = new XAttribute("Id", i); //臨時(shí)緩存每個(gè)每個(gè)站臺(tái)下的開關(guān)狀態(tài) var childList = new List<XElement>(); for (int j = 1; j < 7; j++) { childList.Add(new XElement("SwitchNo", new XAttribute("Id", j), i >= j ? 1 : 0)); } //構(gòu)建一個(gè)站臺(tái)節(jié)點(diǎn) var stationEle = new XElement("Station", xAttr, childList.ToArray()); stationEleList.Add(stationEle); } //構(gòu)建Stations節(jié)點(diǎn) XElement xBody = new XElement("Stations", stationEleList.ToArray()); //構(gòu)建整個(gè)文檔 XDocument doc = new XDocument( new XDeclaration("1.0", "utf-8", "yes"), xBody); doc.Save(fileName); }
2. 讀取當(dāng)前xml 文件并轉(zhuǎn)化為友好的類型
/// <summary> /// 查詢讀取每個(gè)站臺(tái)的開關(guān)狀態(tài)并轉(zhuǎn)化為keyValue鍵值對(duì) /// </summary> /// <returns></returns> public IList<KeyValue<int, int>> GetStationLightData() { writeStationXmlData(); if (stationLightData == null) { stationLightData = new List<KeyValue<int, int>>(); stationSwitchStatus = new List<KeyValue<int, bool>>(); //加載xml string fileName = Path.Combine(dataPath, "StationWitch.xml"); XElement doc = XElement.Load(fileName); //讀取站臺(tái)列表 var station = from s in doc.Descendants("Station") select s; //遍歷站臺(tái) foreach (var st in station) { //獲取每個(gè)站臺(tái)的開關(guān)集合 即SwitchNo節(jié)點(diǎn) var st_switchs = st.Elements("SwitchNo"); var stationId = int.Parse(st.Attribute("Id").Value); int light = 0; foreach (var sw in st_switchs) { var swithNo = int.Parse(sw.Attribute("Id").Value); bool isOn = sw.Value == "1"; if (isOn) light += 20; stationSwitchStatus.Add(new KeyValue<int, bool>(stationId, isOn, swithNo)); } stationLightData.Add(new KeyValue<int, int>(stationId, light)); } } return stationLightData; }
3.更新節(jié)點(diǎn)值(先查詢找到指定的節(jié)點(diǎn) 在更新后保存)
/// <summary> /// 更新制定站臺(tái)中的指定開關(guān)的狀態(tài),即更新制定Station->SwitchNo節(jié)點(diǎn)下值 /// </summary> /// <param name="station"></param> /// <param name="switchNo"></param> /// <param name="isOn"></param> public void UpdateStationSwitchStatus(int station, int switchNo, bool isOn) { string fileName = Path.Combine(dataPath, "StationWitch.xml"); //讀取數(shù)據(jù) XElement doc = XElement.Load(fileName); var up_station = doc.Descendants("Station") .Where(o => o.Attribute("Id").Value == station.ToString()).FirstOrDefault(); //找出指定的節(jié)點(diǎn) var upSwitch = up_station.Elements("SwitchNo").FirstOrDefault(o => o.Attribute("Id").Value == switchNo.ToString()); //更新 if (upSwitch != null) { upSwitch.Value = isOn ? "1" : "0"; } //保存為文件 lock (sync) { doc.Save(fileName); } }
至此,xml的常規(guī)操作就介紹完了,希望能對(duì)有需要的同學(xué)有所幫助.
posted on 2012-07-14 10:15 BoyXiao 閱讀(110) 評(píng)論(0) 編輯 收藏 引用