welcome to Qijie's Blog 薛其杰

I have an XML file stored in DataModel folder, the structure is shown as below:

<?xml version="1.0" encoding="utf-8" ?>
<Bible>
  <Setting>
    <a>1</a>
    <b>2</b>
    <c>3</c>
  </Setting>
  <books>
    <book id="a">
      <display>aaaaaaaa</display>
    </book>
  </books>
</Bible>

In windows phone 8.0, there is a way to use Dom:

using Windows.Storage;
using Windows.Data.Xml.Dom;
        public async void ManipulateXML()
        {
            string file = @"ms-appx:///DataModel/Data.xml";
            Uri url = new Uri(file);
            StorageFile sFile = await StorageFile.GetFileFromApplicationUriAsync(url);

            string stream = await FileIO.ReadTextAsync(sFile);
            XmlDocument xDoc = new XmlDocument();
            xDoc.LoadXml(stream);

            // append node
            var node = xDoc.SelectSingleNode("/Bible/Setting");
            XmlElement ele1 = xDoc.CreateElement("program");
            ele1.InnerText = "this is created by program";
            ele1.SetAttribute("ID", "Third");
            node.AppendChild(ele1);
            await xDoc.SaveToFileAsync(sFile);

            // remove node
            var settings = xDoc.SelectSingleNode("/Bible/Setting");
            var nodes = xDoc.SelectNodes("/Bible/Setting/program");
            int count = nodes.Count;
            for (int i = 0; i < count; i++)
            {
                settings.RemoveChild(nodes[i]);
                await xDoc.SaveToFileAsync(sFile);
            }
        }

 

posted on 2014-11-24 15:54  零点零一  阅读(164)  评论(0编辑  收藏  举报