Windows 8學習筆記(二)——XML檔案的操作

來源:互聯網
上載者:User

今天用了一下Win8 Metro中XML檔案的操作,發現在很有必須整理下來,雖然用法很簡單,但每次用的時候都有些不順手。

        讀取XML檔案

           Windows.Storage.StorageFolder storageFolder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync (Folder);
           Windows.Storage.StorageFile storageFile = await  storageFolder.GetFileAsync(file);
           Windows.Data.Xml.Dom.XmlLoadSettings xmlloadsettings = new XmlLoadSettings();
           xmlloadsettings.ProhibitDtd = false;
           xmlloadsettings.ResolveExternals = false;
           xmlloadsettings.ElementContentWhiteSpace = true;
           XmlDocument doc = await Windows.Data.Xml.Dom.XmlDocument.LoadFromFileAsync(storageFile, xmlloadsettings);

           string xmlData = doc.GetXml();

           顯示如下:

                 

 

           增加節點

           //增加節點
           var nodelist = doc.SelectNodes("/rss/channel");
           for (int i = 0; i < 3; i++)
           {
               XmlElement ele1 = doc.CreateElement("ChannelType");
               XmlElement e1 = doc.CreateElement("ChannelID");
               e1.InnerText = "NO" + i.ToString();
               ele1.AppendChild(e1);
               XmlElement e2 = doc.CreateElement("ChannelName");
               e2.InnerText = "Channel" + i.ToString();
               ele1.AppendChild(e2);

               nodelist[0].AppendChild(ele1);
           }

           await doc.SaveToFileAsync(storageFile);

          結果如下:

          

 

           查詢並修改相應的節點

           var node = nodelist[0].SelectNodes("ChannelType");
           node[0].SelectSingleNode("ChannelID").InnerText = "channel12";

           await doc.SaveToFileAsync(storageFile);

           結果如下:

            

 

           刪除節點

           var nodeMain = nodelist[0].SelectNodes("ChannelType")[0];
           var noderemove = nodeMain.SelectSingleNode("ChannelID");
           nodeMain.RemoveChild(noderemove);

           await doc.SaveToFileAsync(storageFile);

           結果顯示:

            
 

           以上就是XML的簡單操作,排版很彆扭,下了兩個代碼外掛程式卻安裝不上,很是糾結,先將就一下了,我再找找看有沒好的代碼外掛程式,人家的代碼排版很美,而我的呼呼~~

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.