0
我將在我的xml文件上向父節點添加一個新節點。這個代碼的單元測試說「成功」,它通過但沒有添加節點在XML中。如何更新Windows 8 Metro風格的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); }