Software solutions for a mobile world

I have been developing a new WP7 application recently, using XML to store the data. We have used XML extensively in the past to store data and settings, but this is the first 'heavy' use of adding/updating/deleting records using XML files in isolated storage.

Adding the first record was working ok, but adding the second record seemed to corrupt the XML file, so when the application re-started, it failed due to XML file being invalid. So I added a routine, to read the XML file, and display the contents. Sure enough, the file was being corrupted, the header being duplicated, or the wrong portion being deleted.

A sample of an XML file layout that demonstrates this is:-

<?xml version="1.0" encoding="utf-8" ?>
<books>
   <book>
      <id>1</id>
      <title>Book Title 1</title>
      <author>Book Author 1</author>
      <type>Fiction</type>
   </book>
   <book>
      <id>2</id>
      <title>Book Title 2</title>
      <author>Book Author 2</author>
      <type>Fiction</type>
   </book>
 </books>

Our original code for adding a record was:-

 int newId = new_record_id();
 
IsolatedStorageFile isstore = IsolatedStorageFile.GetUserStoreForApplication();
 
IsolatedStorageFileStream bookfile = new IsolatedStorageFileStream("books.xml", System.IO.FileMode.Open, isstore);


 
XDocument xmldetails = XDocument.Load(bookfile);
 
XElement books =
     
new XElement("book",
     
new XElement("id", newId),
     
new XElement("title""Book Title " + newId),
     
new XElement("author""Book Author " + newId),
     
new XElement("type""Fiction"));
     xmldetails.Root.Add(books);
     xmldetails.Save(bookfile);
     bookfile.Close();

 

So, after adding a record, the file was:-

<?xml version="1.0" encoding="utf-8" ?>
<books>
   <book>
      <id>1</id>
      <title>Book Title 1</title>
      <author>Book Author 1</author>
      <type>Fiction</type>
   </book>
   <book>
      <id>2</id>
      <title>Book Title 2</title>
      <author>Book Author 2</author>
      <type>Fiction</type>
   </book>
 </books>
<?xml version="1.0" encoding="utf-8" ?>
<books>
   <book>
      <id>1</id>
      <title>Book Title 1</title>
      <author>Book Author 1</author>
      <type>Fiction</type>
   </book>
   <book>
      <id>2</id>
      <title>Book Title 2</title>
      <author>Book Author 2</author>
      <type>Fiction</type>
   </book>
   <book>
      <id>3</id>
      <title>Book Title 3</title>
      <author>Book Author 3</author>
      <type>Fiction</type>
   </book>
 </books>

We now have 2 sets of XML, and the program quite rightly choked on loading the XML. Similarly, trying to delete record2 would delete record2, but then would duplicate the xml for record1, complete with the header.

I knew it wasn't the code, as I had tested it out in a Windows forms program, so I could easily check the XML output. So, after lots of judicious searching for a solution, checking and re-checking the code, I finally came across this thread in the old Windows Phone forums. I tried setting the position to 0 after reading the file, but it seemed to make no difference. I then tried the suggestion of deleting the file, and then saving it. So the insert code now becomes with the additional lines in bold.

   int newId = new_record_id();
  IsolatedStorageFile isstore = IsolatedStorageFile.GetUserStoreForApplication();
  IsolatedStorageFileStream bookfile = new IsolatedStorageFileStream("books.xml", System.IO.FileMode.Open, isstore);
  XDocument xmldetails = XDocument.Load(bookfile);

  XElement books = new XElement("book",
       new XElement("id", newId),
       new XElement("title", "Book Title " + newId),
       new XElement("author", "Book Author " + newId),
       new XElement("type", "Fiction"));
       xmldetails.Root.Add(books);

  bookfile.Close();
  isstore.DeleteFile("
books.xml");
  bookfile =
new IsolatedStorageFileStream("books.xml", System.IO.FileMode.Create, isstore);
  xmldetails.Save(bookfile);
  bookfile.Close();

This works for inserts, updates and deletes.

To demonstrate the problem, and the fix, I have written a sample program, which wll create an XML file, and allow updates, inserts and deletes of records. To re-generate the XML file, click on the 'Create' button. After doing an operation, the program will either display the results of a query, or display the error. Check the 'Fix XML' box to run the corrected code. The sample is downloadable from here

The sample should also illustrates simple XML file manipulation in Windows Phone 7.

Originally posted 6/Dec/2010 

Contact

M: +44 (0)7860 907493
B:  +44 (0)1204 657497

pete@gui-innovations.com

More

Connections

   

Twitter Feed