IndependentFileStorage XML 读取崩溃
好吧,基本上我的问题是从IsolatedFileStorage 读取XML 文件。我将详细介绍导致错误的过程,然后列出相关代码和 XML 文件。
- 在第一次执行时,它认识到该文件不存在 - 因此它在isolatedfilestorage中创建该文件
- 在第二次执行时,它现在可以看到该文件确实存在,因此它加载了XML文件
- 在第三次执行时,它可以看到它存在- 但它抛出了一个 XML 错误
,我一生都找不到解决方案(链接到 MSDN 上的其他讨论 这里)
因此,读取/创建IsolatedFileStorage中的XML文件的代码如下:
try
{
/***********************
* CHECK THE SETTINGS
********************/
if (store.FileExists("AppSettings.xml"))
{
streamSettings = new IsolatedStorageFileStream("AppSettings.xml", System.IO.FileMode.Open, store);
DebugHelp.Text = "AppSettings.xml exists... Loading!";
streamSettings.Seek(0, System.IO.SeekOrigin.Begin);
xmlDoc = XDocument.Load(streamSettings, LoadOptions.None);
}
else
{
streamSettings = new IsolatedStorageFileStream("AppSettings.xml", System.IO.FileMode.Create, store);
DebugHelp.Text = "AppSettings.xml does not exist... Creating!";
xmlDoc = XDocument.Load("AppSettings.xml", LoadOptions.None);
}
if (xmlDoc != null)
xmlDoc.Save(streamSettings);
}
catch (Exception e)
{
DebugHelp.Text = e.ToString();
}
finally
{
streamSettings.Close();
}
相关的XML文件如下:
<?xml version="1.0" encoding="utf-8" ?>
<Settings>
</Settings>
我知道非常先进 - 但是它会引发以下错误(此处),您可以找到完整的Social.MSDN 页面底部的错误文本。
请帮忙 - 我已经寻找解决方案(因为 Social.msdn 网站上的解决方案不起作用)大约两周了。
Ok so, basically my problem is with reading and XML file from IsolatedFileStorage. I'll go through the process that leads to my error and then I'll list the relevant code and XML file.
- On the first execution it recognises that the file does not exist - it therefore creates the file in IsolatedFileStorage
- On the second execution it can now see that the file does exist and so it loads the XML file
- On the third execution it can see that it exists - but it throws an XML error
I cannot for the life of me find a solution to it (link to other discussion on MSDN here)
So the code for reading/creating the XML file in IsolatedFileStorage is as follows:
try
{
/***********************
* CHECK THE SETTINGS
********************/
if (store.FileExists("AppSettings.xml"))
{
streamSettings = new IsolatedStorageFileStream("AppSettings.xml", System.IO.FileMode.Open, store);
DebugHelp.Text = "AppSettings.xml exists... Loading!";
streamSettings.Seek(0, System.IO.SeekOrigin.Begin);
xmlDoc = XDocument.Load(streamSettings, LoadOptions.None);
}
else
{
streamSettings = new IsolatedStorageFileStream("AppSettings.xml", System.IO.FileMode.Create, store);
DebugHelp.Text = "AppSettings.xml does not exist... Creating!";
xmlDoc = XDocument.Load("AppSettings.xml", LoadOptions.None);
}
if (xmlDoc != null)
xmlDoc.Save(streamSettings);
}
catch (Exception e)
{
DebugHelp.Text = e.ToString();
}
finally
{
streamSettings.Close();
}
And the related XML file is as follows:
<?xml version="1.0" encoding="utf-8" ?>
<Settings>
</Settings>
Extremely advanced I know - however it throws the following error (here) and you can find the full error text at the bottom of the Social.MSDN page.
Please help - I have been looking for a solution (as the one on the social.msdn site didn't work) for about 2 weeks now.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么不尝试使用简单的 StreamReader 读取文件?下面是我创建的从商店读取文件的方法的一部分。尝试一下,检查您的内容,然后尝试从 String 加载 xml(XDocument.Parse 等...)
Why don't you try to read file using a simple StreamReader ? Below a part of a method I have created to readfile from store. Have a try, check your content, and then try loading xml from String (XDocument.Parse etc ...)
在我看来,问题出在您的保存方法中 - 看起来您可能会在每次关闭时附加设置 - 要覆盖现有设置,您需要确保删除现有文件并创建一个新文件。
为了帮助调试此问题,请尝试使用 http://wp7explorer.codeplex.com/ - 这可能会帮助您查看原始文件“在磁盘上”
顺便说一句,对于一般设置,请检查IsolatedStorage默认提供的AppSettings - 除非您有复杂的需求,否则这些本身就足够了。
It looks to me like the problem is in your save method - it looks like you are maybe appending the settings each time you close - to overwrite your existing settings, you need to ensure that you delete your existing file and create a new one.
To help debug this, try using http://wp7explorer.codeplex.com/ - this might help you see the raw file "on disk"
As an aside, for settings in general, do check out the AppSettings that IsolatedStorage provides by default - unless you have complicated needs, then these may suffice on their own.
您的代码示例不完整,因此很难确定,但是,您可能会发现,如果文件已经存在,则删除它比仅查找文件的开头更容易。您可以使用
FileMode.Create
来完成此操作。反过来,这意味着您可以无需检查现有文件。我怀疑问题在于您在后续尝试中向文件写入了少量文本,因此留下了部分原始/先前文本。这又会创建一个包含无效 XML 的文件。
Your code sample isn't complete so it's hard to say for sure but, rather than just seeking to the start of the file you may find it easier to just delete it if it already exists. You can do this with
FileMode.Create
. In turn this means you can do away with the need to check for the existing file.I suspect that the problem is that you are writing a smaller amount of text to the file on subsequent attempts and so leaving part of the original/previous text behind. In turn this creates a file which contains invalid XML.