这是我更改 XML 元素属性值的方法:
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
XDocument xml = null;
using (IsolatedStorageFileStream isoFileStream = myIsolatedStorage.OpenFile("Stats_file.xml", FileMode.Open, FileAccess.Read))
{
xml = XDocument.Load(isoFileStream, LoadOptions.None);
xml.Element("statrecords").SetElementValue("value", "2"); //nullreferenceexception
}
using (IsolatedStorageFileStream isoFileStream = myIsolatedStorage.OpenFile("Stats_file.xml", FileMode.Truncate, FileAccess.Write))
{
xml.Save(isoFileStream, SaveOptions.None);
}
}
在第 7 行我有 NullReferenceException。你知道如何无误地改变价值吗?
这是我的 XML 文件:
<?xml version='1.0' encoding='utf-8' ?>
<stats>
<statmoney index='1' value='0' alt='all money' />
<statrecords index='2' value='0' alt='all completed records' />
</stats>