简体   繁体   中英

How can i update a node of xml file on Windows 8 Metro style?

I will add a new node to parent node on my xml file. Unit test for this code says "success", it is passing but no added node in xml.

Also, some of tests i'm getting the error; << Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)) >> on the line SaveToFileAsync

what is wrong with me? and there is an alternative way to do this?

    public string ConnectionPath { get; set; }
    protected string XPath { get; set; }
    protected string XParentPath { get; set; }
    protected XmlDocument Source { get; set; }
    protected StorageFolder SFolder { get; set; }
    protected StorageFile SFile { get; set; }

    public RepositoryBase(string connectionPath)
    {
        this.ConnectionPath = connectionPath;
    }

     public async void Insert(T entity)
     {
        SFolder = Package.Current.InstalledLocation;
        SFile = await SFolder.GetFileAsync(this.ConnectionPath);
        var content = await FileIO.ReadTextAsync(SFile);
        if (!string.IsNullOrEmpty(content))
        {
            Source = new XmlDocument();
            Source.LoadXml(content);
        }
        var tagName = typeof(T).Name;
        if (tagName != null)
        {
            IXmlNode parentNode = Source.SelectSingleNode(XParentPath);
            if (parentNode != null)
            {
                XmlElement newNode = Source.CreateElement(tagName);
                newNode.InnerText = GetContent(entity);
                parentNode.AppendChild(newNode);
            }              
        }        
        await Source.SaveToFileAsync(SFile);
     }

* PlaceRepositoryClass ;

    public class PlaceRepository : RepositoryBase<Place>
    {
        public PlaceRepository()
        : base("Data\\bla\\bla.xml")
        {

             XPath = "/Country[Id=1]/Cities/City[Id=1]/Places/Place";
             XParentPath = "/Country[Id=1]/Cities/City[Id=1]/Places";
         }
     }
  • Unit Test Method ;

     [TestMethod] public void AppendNode() { Place place = new Place() { Id = 40, Name = "xxxxx", SummaryPath = "yyyyy", Logo = "xy.png", LogoSmall = "xy_small.png", Latitude = "32.423", Longitude = "34.23424", Content = new PlaceContent() { Items = new List<ContentItem>() { new ContentItem() { TextPath = "aaaa", Header = "bbbbb", AudioFilePath = "x.mp3" } } }, Gallery = new PhotoGallery() { Photos = new List<Photo>() { new Photo() { Path = "ab.png", Text = "abab" } } } }; PlaceRepository repository = new PlaceRepository(); repository.Insert(place); } 

You are trying to write to a file that's part of the application package, and that's read only. You could copy the file to local storage and make the modifications there perhaps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM