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