unable to update value to xml file in windows phone 8



I want to set the attributes pro_id and prod_xml to empty whenever I press on unfavorite icon from application bar. After all, the values were to set to empty however, it also add one more root element to the xml file like this



<?xml version="1.0" encoding="utf-8"?>
<favorites>
<favorite id="1" pro_id="" prod_xml="" />
<favorite id="2" pro_id="" prod_xml="" />
<favorite id="3" pro_id="" prod_xml="" />
<favorite id="4" pro_id="" prod_xml="" />
</favorites> />
</favorites>


Here my code to do this



public void UnwriteXML(string pro_id)
{
var storage = IsolatedStorageFile.GetUserStoreForApplication();
fileName = "Favorite\\Favorite.xml";
XDocument docx = null;
using (IsolatedStorageFileStream isoStreamx = new IsolatedStorageFileStream(fileName, FileMode.Open, storage))
{

docx = XDocument.Load(isoStreamx);
foreach (var item in (from item in docx.Descendants("favorite")
where item.Attribute("pro_id").Value.Equals(pro_id)
select item).ToList())
{

item.Attribute("pro_id").SetValue("");
item.Attribute("prod_xml").SetValue("");

}


//First way
isoStreamx.Position = 0;
docx.Save(isoStreamx);
}

}


I followed exactly the same to question post from here updating an existing xml file in Windows Phone by setting isoStream to position 0 because I think he have a problem like me, but the problem is still the same, I don't understand what's wrong with this.


No comments:

Post a Comment