• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Zachary.XiaoZhen - 夢想的天空

            Linq to Xml 小結

            Linq to xml 這個東西出來好多年了,但一直沒有機會在項目中用到,前段時間,終于項目中一些地方需要用到xml作為數據源,于是就體驗了一把這個,感覺還挺不錯的,今天在此小結一下.

                   首先我們來模擬一下一個真實的業務場景:在這里我們需要展示一個火車站上各個站臺上面燈光開關狀態.假設每個站臺一共6個開關,站臺我們用Station節點表示,站臺開關我們用SwitchNo來表示,在初始化(即默認狀態)的時候,我們需要表示成第一個站臺2個開關開著,第二個站臺3個開關開著,第三個站臺4個開關開著….

                那么我們可能需要的一個xml文件格式可能就是如下這個樣子: 

            <?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數據的創建/查詢/修改這三個方面簡單談一下吧.關于概念啥的就不啰嗦了,直接上代買吧.

            1. 創建這個文件

            /// <summary>
                    /// 創建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>();  //臨時緩存每個站臺節點
            
                        //一共6個站臺,循環創建           
                        for (int i = 1; i < 7; i++)
                        {
                            var xAttr = new XAttribute("Id", 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));
                            }
            
                            //構建一個站臺節點
                            var stationEle = new XElement("Station", xAttr, childList.ToArray());
                            stationEleList.Add(stationEle);
                        }
            
                        //構建Stations節點
                        XElement xBody = new XElement("Stations", stationEleList.ToArray());
            
                        //構建整個文檔
                        XDocument doc = new XDocument(
                                new XDeclaration("1.0", "utf-8", "yes"), xBody);
                        doc.Save(fileName);
                    }

            2. 讀取當前xml 文件并轉化為友好的類型

            /// <summary>
                    /// 查詢讀取每個站臺的開關狀態并轉化為keyValue鍵值對
                    /// </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);
            
                            //讀取站臺列表
                            var station = from s in doc.Descendants("Station")
                                          select s;
                            //遍歷站臺
                            foreach (var st in station)
                            {
                                //獲取每個站臺的開關集合 即SwitchNo節點
                                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.更新節點值(先查詢找到指定的節點 在更新后保存)

             /// <summary>
                    /// 更新制定站臺中的指定開關的狀態,即更新制定Station->SwitchNo節點下值
                    /// </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");
                        //讀取數據
                        XElement doc = XElement.Load(fileName);
                        var up_station = doc.Descendants("Station")
                            .Where(o => o.Attribute("Id").Value == station.ToString()).FirstOrDefault();
            
                        //找出指定的節點
                        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的常規操作就介紹完了,希望能對有需要的同學有所幫助.

            posted on 2012-07-14 10:15 BoyXiao 閱讀(103) 評論(0)  編輯 收藏 引用

            導航

            留言簿(43)

            最新評論

            閱讀排行榜

            評論排行榜

            91久久福利国产成人精品| 少妇人妻88久久中文字幕| 国产精品久久久99| 青青青青久久精品国产h久久精品五福影院1421| 久久99精品国产99久久| 成人a毛片久久免费播放| 开心久久婷婷综合中文字幕| 亚洲国产日韩欧美久久| 久久一日本道色综合久久| 久久综合狠狠色综合伊人| 亚洲国产成人久久综合一区77| 国产色综合久久无码有码| 伊人丁香狠狠色综合久久| 久久久久久曰本AV免费免费| 久久婷婷国产麻豆91天堂| 一本色道久久99一综合| 国内精品久久久久久久涩爱| 亚洲国产精品无码久久| 亚洲人成无码网站久久99热国产| av午夜福利一片免费看久久| 久久夜色精品国产www| 日韩亚洲欧美久久久www综合网| 久久伊人精品一区二区三区| 精品久久久久中文字幕一区| 国产成人精品久久一区二区三区| 久久午夜夜伦鲁鲁片免费无码影视| 大美女久久久久久j久久| 久久精品人人做人人妻人人玩| 久久久久亚洲国产| 一级a性色生活片久久无少妇一级婬片免费放 | 久久久无码一区二区三区| 欧美精品九九99久久在观看| 精品久久久久中文字| 亚洲国产天堂久久综合网站| 国产一区二区三区久久精品| 久久久久人妻一区精品色| 亚洲中文字幕无码久久综合网| 日本五月天婷久久网站| 久久久久久国产a免费观看黄色大片 | 亚洲精品国产美女久久久| 精品久久久久久国产|