Removing XElement in Windows Phone 7 XML isolated storage file

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi,
I have two functions to update an XML
======
REMOVE
======

<div style="color:Black;background-color:White; <pre>
<span style="color:Green; //Deleting a specific station
<span style="color:Blue; private <span style="color:Blue; void RemoveStation(<span style="color:Blue; string idA)
{
IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication();
<span style="color:Blue; try
{

<span style="color:Blue; using (storage)
{
<span style="color:Blue; if (storage.FileExists(<span style="color:#A31515; "fave.xml"))
{
<span style="color:Green; //Reading and deleting the station
IsolatedStorageFileStream fileStream = storage.OpenFile(<span style="color:#A31515; "fave.xml", FileMode.Open, FileAccess.ReadWrite);
XDocument _doc = XDocument.Load(fileStream);
<span style="color:Blue; var item = <span style="color:Blue; from c <span style="color:Blue; in _doc.Element(<span style="color:#A31515; "stations").Elements(<span style="color:#A31515; "station")
<span style="color:Blue; where c.Attribute(<span style="color:#A31515; "id").Value == idA
<span style="color:Blue; select c;

item.Remove();
fileStream.Dispose();

<span style="color:Green; //Updating the file
IsolatedStorageFileStream location = <span style="color:Blue; new IsolatedStorageFileStream(<span style="color:#A31515; "fave.xml", FileMode.Open, FileAccess.ReadWrite, storage);
System.IO.StreamWriter file = <span style="color:Blue; new System.IO.StreamWriter(location);
_doc.Save(file);
file.Dispose();
location.Dispose();

}
<span style="color:Blue; else
{
Console.WriteLine(<span style="color:#A31515; "fave.xml not found");
}
}
} <span style="color:Blue; catch (Exception e)
{
Console.WriteLine(<span style="color:#A31515; "Error P-RS: Error opening storage");
Console.WriteLine(e.StackTrace);
} <span style="color:Blue; finally
{
storage.Dispose();
}
}
[/code]

=======
SAVE
=======

<div style="color:Black;background-color:White; <pre>
<span style="color:Green; //Adding a specific station
<span style="color:Blue; private <span style="color:Blue; void SaveStation(<span style="color:Blue; string idA)
{
<span style="color:Green; /* Sample XML
* <station id="xxxxxx" name="ABC Station" />
*/
IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication();

<span style="color:Blue; try
{
<span style="color:Blue; using (storage)
{
<span style="color:Blue; if (storage.FileExists(<span style="color:#A31515; "fave.xml"))
{
<span style="color:Green; //Reading the file
IsolatedStorageFileStream fileStream = storage.OpenFile(<span style="color:#A31515; "fave.xml", FileMode.Open, FileAccess.ReadWrite);
XDocument _doc = XDocument.Load(fileStream);
XElement _item = <span style="color:Blue; new XElement(<span style="color:#A31515; "station");
XAttribute id = <span style="color:Blue; new XAttribute(<span style="color:#A31515; "id", IDTextBlock.Text);
XAttribute name = <span style="color:Blue; new XAttribute(<span style="color:#A31515; "name", NameTextBlock.Text);
_item.Add(id, name);
_doc.Root.Add(<span style="color:Blue; new XElement(_item));
fileStream.Dispose();

<span style="color:Green; //Updating the file
IsolatedStorageFileStream location = <span style="color:Blue; new IsolatedStorageFileStream(<span style="color:#A31515; "fave.xml", FileMode.Open, FileAccess.ReadWrite, storage);
System.IO.StreamWriter file = <span style="color:Blue; new System.IO.StreamWriter(location);
<span style="color:Green; //System.IO.StreamWriter file = new System.IO.StreamWriter(fileStream);
_doc.Save(file);
file.Dispose();
location.Dispose();

}
}
}
[/code]

Both functions are working. SAVE function works properly and you can insert data after data. However REMOVE function seems to corrupt the XML. Thus after REMOVE, the XML is unreadable anymore, eventhough when I debug _doc variable seems to have the correct
data
For example

<ol>
Adding Station A ------------------- (OK) Adding Station B ------------------- (OK) Removing Station A --------------- (OK) the functions executes fine, but i think at this point the XML is corrupted
Adding Station C ------------------- (ERROR), something wrong with the XML root level bla bla bla
</ol>
Help please :)
<br/>

Thank you very much


View the full article
 
Back
Top