I want to store data that i get from a url into a xml file and show that data when there is no internet.
I have this:
async Task CreateSaveFile()
{
// Create document
XDocument xmlDocument = new XDocument(new XDeclaration("1.0", "utf-8", "yes"),
new XElement("Objects", from test in TestList
select new XElement("test",
new XElement("name", test.Name),
new XElement("number", test.Number))));
// Storage Folder
StorageFolder folder = Windows.Storage.ApplicationData.Current.LocalFolder;
string fileName = "test.xml";
StorageFile file = await folder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
Stream stream = await file.OpenStreamForWriteAsync();
// Write file
xmlDocument.Save(stream);
stream.Flush();
}
And this method to get the data from the created test.xml into a list:
private List<Test> testList = new List<Test>();
async Task<List<Test>> getTestList()
{
StorageFolder folder = Windows.Storage.ApplicationData.Current.LocalFolder;
IReadOnlyList<StorageFile> files = await folder.GetFilesAsync();
foreach (StorageFile file in files)
{
// Get xml files
XDocument xmlFile = XDocument.Load(file.Path);
// Get TestObjects
testList = (from element in xmlFile.Root.Descendants("test")
select new Player(
element.Element("name").Value,
element.Element("number").Value)).ToList<Test>();
}
return testList;
}
I get this error An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in System.Windows.ni.dll
No comments:
Post a Comment