在过去的几个小时里,我一直在为Windows Phone 7上的LINQ to XML而苦苦挣扎。我只是想向现有的XML文件中添加新元素。
XElement newItem = new XElement("item",new XAttribute("id",3),
new XElement("content","Buy groceries"),
new XElement("status","false"));
IsolatedStorageFile isFile = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream stream = new IsolatedStorageFileStream("/Items.xml", System.IO.FileMode.Open, isFile);
XDocument loadedDoc = XDocument.Load(stream);
loadedDoc.Add(newItem);
loadedDoc.Save(stream);
通常我遇到非常奇怪的行为。有时会出现“在
IsolatedStorageFileStream
上不允许操作”的错误。有时是“缺少根元素”,有时是“意外的XML声明。 XML声明必须是文档中的第一个节点,并且不允许在其之前出现任何空格字符。”例如,如果我将System.IO.FileMode.Open
更改为Create, 我会得到“缺少根元素”的错误,但除此之外似乎没有任何错误发生的模式。
这一切似乎都是由我的XML文件引起的问题:
<?xml version="1.0" encoding="utf-8" ?>
<items>
<item id="1">
<content>Get groceries</content>
<status>false</status>
</item>
<item id="2">
<content>Wash your car</content>
<status>true</status>
</item>
</items>
这没有比这更简单的了,我非常确定在实际的XML文件中声明之前没有任何空格。为了使所有的事情变得更加奇怪,我下载了一个使用相同写入XML文件技术的小示例应用程序。当我尝试运行它时,我收到了一个非常熟悉的错误消息:“意外的XML声明。XML声明必须是文档中的第一个节点,并且不允许在其前面出现任何空白字符。” 这里是链接到该主题的链接: http://mobileworld.appamundi.com/blogs/petevickers/archive/2010/12/06/windows-phone-7-linq-to-xml-corruption.aspx/ 昨天我安装了Windows Phone SDK 7.1 BETA。那可能会引起问题吗?
似乎问题出在无法定位独立存储中的xml文件上。我已经创建了全新的项目和新的xml文件。
这是我唯一的代码:
// Constructor
public MainPage()
{
InitializeComponent();
var isFile = IsolatedStorageFile.GetUserStoreForApplication();
var stream = isFile.OpenFile("file.xml", System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite);
}
我仍然收到“在IsolatedStorageFileStream上不允许的操作”的错误。我尝试进入file.xml文件的属性,更改构建操作和复制到输出目录,但没有起作用。我尝试添加:
isFile.Dispose();
我还尝试添加条件语句:
if (isFile.FileExists("file.xml"))...
但他找不到这个文件。
我从未在生活中遇到过如此困境,请给我一些建议,还有什么其他的尝试方法吗?