I am trying to delete a node if name is corresponding but I get a NullReferenceException
in foreach
statement. Here is the xml structure:
<Items>
<channel>
<region>test</region>
<name>testname</name>
<oldposition>0</oldposition>
<newpostions>0</newpostions>
</channel>
</Items>
And here is how I try to delete the node:
private void removeFromXml(string buttonname)
{
XDocument doc;
using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
{
if (isoStore.FileExists("favorites.xml"))
{
using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream("favorites.xml", FileMode.Open, isoStore))
{
doc = XDocument.Load(isoStream);
}
Debug.WriteLine(doc.ToString());
var q = from b in doc.Descendants("channel") select b;
foreach (XElement xe in q) //here is the error described below
{
if (xe.Element("name").Value.ToString() == buttonname)
{
xe.Remove();
Button bt = (Button)favorites1.FindName(buttonname + "fav");
favorites1.Items.Remove(bt);
}
}
using (IsolatedStorageFileStream isoStream =
new IsolatedStorageFileStream("favorites.xml", FileMode.Create, isoStore))
{
doc.Save(isoStream, SaveOptions.OmitDuplicateNamespaces);
}
}
}
}
I get this error on foreach
statement :
An exception of type 'System.NullReferenceException
' occurred in System.Xml.Linq.ni.dll
but was not handled in user code
Additional information: Object reference not set to an instance of an object.
No comments:
Post a Comment