C# 创建一个xml文件来存储本地c的数据,并从创建的文件中读取数据
我想将从url获取的数据存储到xml文件中,并在没有internet的情况下显示这些数据 我有这个:C# 创建一个xml文件来存储本地c的数据,并从创建的文件中读取数据,c#,silverlight,C#,Silverlight,我想将从url获取的数据存储到xml文件中,并在没有internet的情况下显示这些数据 我有这个: async Task CreateSaveFile() { // Create document XDocument xmlDocument = new XDocument(new XDeclaration("1.0", "utf-8", "yes"), new XElement("Objects", from test in T
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();
}
此方法用于将创建的test.xml中的数据获取到列表中:
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;
}
这看起来像是windows应用商店应用程序的过程,Silverlight有一个不同的api。有关读取和写入Silverlight隔离存储的信息,请参阅此信息。的完整ToString输出是什么,包括InnerException?我是认真的。使用try{}catch exception ex{Debug.WriteLineex.ToString;throw;}临时捕获异常以查看完整异常,或者和。然后让我们知道它是什么。它只需转到调试处理程序私有无效应用程序\u UnhandledExceptionobject发送方,ApplicationUnhandledExceptionEventTargets{如果Debugger.IsAttached{//发生了未经处理的异常;进入调试器Debugger.break;}}ApplicationUnhandledExceptionEventTargets具有属性ExceptionObject。请尝试将Debug.WriteLinee.ExceptionObject.ToString的输出添加到您的问题中。问题是我不是.Net专家tbh,我不知道在何处或如何:
public RelayCommand LoadedCommand
{
get
{
return new RelayCommand(async () =>
{
await CreateSaveFile();
try
{
TestList = await getTestList();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString()); throw;
}
});
}
}