Windows 8 学习笔记(二)——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();

           显示如下:

          201203302214568452.png

 

 增加节点

           //增加节点
           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);

          结果如下:

          201203302214581601.jpg 

  查询并修改相应的节点

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

           await doc.SaveToFileAsync(storageFile);

           结果如下:

    201203302215004435.jpg  

 删除节点

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

           await doc.SaveToFileAsync(storageFile);

           结果显示:

    201203302215027825.jpg


 
原文地址:http://www.devdiv.com/Windows_8学习笔记(二)——XML文件的操作-thread-130269-1-15.html

posted @ 2012-09-29 09:37  @大龙哥  阅读(279)  评论(0编辑  收藏  举报