0
我正在创建一个Windows Phone 7应用程序,并且在更改xml文件(位于独立存储内部)中的值时遇到了一些问题。 我的方法是在这里:如何修改存储在独立存储器内的xml文件中的值?
public void updateItemValueToIsoStorage(string id,
string itemAttribute,
string value)
{
using (var isoStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (var stream = isoStorage.OpenFile(
"items.xml", FileMode.Open, FileAccess.ReadWrite))
{
XDocument xml = XDocument.Load(stream, LoadOptions.None);
//According to given parameters,
//set the correct attribute to correct value.
var data = from c in xml.Descendants("item")
where c.Attribute("id").Value == id
select c;
foreach (Object i in data)
{
xml.Root.Attribute(itemAttribute).SetValue(value);
}
}
}
}
和孤立的存储在我的XML文件是这样的:
<?xml version="1.0" encoding="utf-8"?>
<items>
<item id="0" title="Milk" image="a.png" lastbought="6" lastingtime="6" />
<item id="1" title="Cheese" image="b.png" lastbought="2" lastingtime="20" />
<item id="2" title="Bread" image="c.png" lastbought="3" lastingtime="8" />
</items>
我得到这一行一个NullReferenceException:
xml.Root.Attribute(itemAttribute).SetValue(value);
任何想法如何那我该怎么做? 干杯。
是的,它似乎工作。我不知道我在那里有什么样的脑冻结。谢谢。 – Baburo 2011-12-18 17:14:12