【发布时间】:2014-06-01 01:56:43
【问题描述】:
当应用程序恢复时,我正在尝试在 C# WinRt 应用程序中读取 XML 文件:
Windows.Storage.StorageFile File = await Windows.Storage.ApplicationData.Current.TemporaryFolder.GetFileAsync("PreviousSession.xml");
if (File != null)
{
var File2 = await Windows.Storage.ApplicationData.Current.TemporaryFolder.GetFileAsync("PreviousSession.xml");
string Document = File2.ToString();
System.Xml.Linq.XDocument.Parse(Document);
}
但我得到了System.Xml.XmlException:
Data at the root level is invalid. Line 1, position 1.
如何解决这个问题并正确读取文件?
我的 XML 文档是这样构造的:
Windows.Data.Xml.Dom.XmlDocument Document = new Windows.Data.Xml.Dom.XmlDocument();
Windows.Data.Xml.Dom.XmlElement Element = (Windows.Data.Xml.Dom.XmlElement)Document.AppendChild(Document.CreateElement("PreviousSessionData"));
...
Windows.Storage.IStorageFile TempFile = await Windows.Storage.ApplicationData.Current.TemporaryFolder.CreateFileAsync("PreviousSession.xml", Windows.Storage.CreationCollisionOption.ReplaceExisting);
await Document.SaveToFileAsync(TempFile);
对于这样的文件:
<PreviousSessionData>...</PreviousSessionData>
【问题讨论】:
-
请显示您的 XML 文件,至少前几行。
标签: c# xml windows-runtime linq-to-xml