我如何更新一个节点的xml文件在Windows 8 Metro风格

本文关键字:文件 xml 风格 Metro Windows 一个 何更新 更新 节点 | 更新日期: 2023-09-27 17:50:31

我将在xml文件的父节点上添加一个新节点。这段代码的单元测试显示"成功",它通过了,但没有在xml中添加节点。

另外,一些测试我得到的错误;& lt; & lt;访问被拒绝。(Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))>>SaveToFileAsync

我怎么了?还有别的办法吗?

    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);
     }

* placepositoryclass;

    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";
         }
     }
  • 单元测试方法;

    [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);
    }
    

我如何更新一个节点的xml文件在Windows 8 Metro风格

您正在尝试写入一个文件,该文件是应用程序包的一部分,并且是只读的。您可以将文件复制到本地存储,并在那里进行修改。