C# 如何在Windows 8 Metro样式上更新xml文件的节点?
我将向xml文件上的父节点添加一个新节点。此代码的单元测试显示“成功”,它正在通过,但没有在xml中添加节点 还有,一些测试我得到了错误; > 在线SaveToFileAsync 我怎么了?还有别的方法吗C# 如何在Windows 8 Metro样式上更新xml文件的节点?,c#,windows-8,microsoft-metro,C#,Windows 8,Microsoft Metro,我将向xml文件上的父节点添加一个新节点。此代码的单元测试显示“成功”,它正在通过,但没有在xml中添加节点 还有,一些测试我得到了错误; > 在线SaveToFileAsync 我怎么了?还有别的方法吗 public string ConnectionPath { get; set; } protected string XPath { get; set; } protected string XParentPath { get; set; } protecte
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";
}
}
公共类PlaceRepository:RepositoryBase
{
公共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); }
[TestMethod] public-void-AppendNode() { 地点=新地点() { Id=40, Name=“xxxxx”, SummaryPath=“yyyy”, Logo=“xy.png”, logosmal=“xy_small.png”, 纬度=“32.423”, 经度=“34.23424”, Content=newplacecontent(){Items=newlist(){newcontentitem(){TextPath=“aaaa”,Header=“bbbbb”,AudioFilePath=“x.mp3”}}, Gallery=新照片库(){Photos=新列表(){newphoto(){Path=“ab.png”,Text=“abab”}} }; PlaceRepository repository=新的PlaceRepository(); 储存库。插入(位置); }