In my application I am creating XML in App.xaml.cs like this:
public async static void CreateBadgesXML()
{
StorageFolder sf = await ApplicationData.Current.LocalFolder.CreateFolderAsync("Data", CreationCollisionOption.OpenIfExists);
StorageFile st = await sf.CreateFileAsync("Badgess.xml", CreationCollisionOption.ReplaceExisting);
XmlDocument dom = new XmlDocument();
XmlElement x;
x = dom.CreateElement("badges");
dom.AppendChild(x);
XmlElement x1 = dom.CreateElement("badge");
XmlElement x11 = dom.CreateElement("id");
x11.InnerText = "1";
x1.AppendChild(x11);
XmlElement x12 = dom.CreateElement("name");
x12.InnerText = "Badge One";
x1.AppendChild(x12);
XmlElement x13 = dom.CreateElement("pictureurl");
x13.InnerText = "two.png";
x1.AppendChild(x13);
XmlElement x14 = dom.CreateElement("isachieved");
x14.InnerText = "false";
x1.AppendChild(x14);
x.AppendChild(x1);
await dom.SaveToFileAsync(st);
}
Next in MainViewModel I am binding these data using reposiory pattern to ObservableCollection like this:
IList<Badge> list = await _badgeService.GetAll();
BadgesList = list.ToObservableCollection<Badge>();
But when I am going to edit this XML in another View which is connected to another VievModel (QuestionViewModel) like this:
public async Task Update(int id)
{
StorageFolder sf = await ApplicationData.Current.LocalFolder.CreateFolderAsync("Data", CreationCollisionOption.ReplaceExisting);
StorageFile st = await sf.CreateFileAsync("Badgess.xml", CreationCollisionOption.ReplaceExisting);
XmlDocument dom = new XmlDocument();
XmlElement x;
x = dom.CreateElement("badges");
dom.AppendChild(x);
XmlElement x1 = dom.CreateElement("badge");
XmlElement x11 = dom.CreateElement("id");
x11.InnerText = "1";
x1.AppendChild(x11);
XmlElement x12 = dom.CreateElement("name");
x12.InnerText = "Badge One";
x1.AppendChild(x12);
XmlElement x13 = dom.CreateElement("pictureurl");
x13.InnerText = "two.png";
x1.AppendChild(x13);
XmlElement x14 = dom.CreateElement("isachieved");
x14.InnerText = "true";
x1.AppendChild(x14);
x.AppendChild(x1);
await dom.SaveToFileAsync(st);
}
And sending a Message from QuestionViewModel to MainViewModel like this:
Messenger.Default.Send<CleanUp>(new CleanUp());
And try to bind this collection again:
private void CallCleanUp(CleanUp obj)
{
BadgesList = null;
GetBadges();
}
I get an error XmlException "Root element is missing." I do not understand why after XML update, content of this file is empty? Any ideas?
EDIT
After changes in update method, now I've got an UnauthorizedAccessException in GetAll()
method in my BadgeService, still strange because in appears only after update XML.