0

我将在我的 xml 文件的父节点中添加一个新节点。此代码的单元测试显示“成功”,它正在通过但在 xml 中没有添加节点。

此外,一些测试我得到了错误;<< 访问被拒绝。(来自 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);
     }

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

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

1 回答 1

1

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

于 2013-01-03T23:23:28.140 回答