Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WP7 Read Write Xml in IsolatedStorage

I am new to WP7. I followed this tutorial to read and write a xml file but when i read the xml file it only shows me the top row of xml file.I don't know how to check weather the xml file is written properly by the program.So.

1.Where to check the xml files that are saved in isolated storage.

2.How to get out of this problem.

My code to Write Xml File In Isolated Storage:

      using (IsolatedStorageFile myIsolatedStorage =     
                            IsolatedStorageFile.GetUserStoreForApplication())
        {
            using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream("mz1.xml", FileMode.Create, myIsolatedStorage))
            {
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent = true;
                using (XmlWriter writer = XmlWriter.Create(isoStream, settings))
                {
                    writer.WriteStartDocument();

                    writer.WriteStartElement("person");
                    writer.WriteElementString("node1", "value1");
                    writer.WriteEndElement();
                    writer.WriteEndDocument();
                    writer.Flush();
                }
            }
        }

Code to Read Xml File From Isolated Storage:

          using (IsolatedStorageFile myIsolatedStorage =          
                               IsolatedStorageFile.GetUserStoreForApplication())
            {
                IsolatedStorageFileStream isoFileStream =  
                         myIsolatedStorage.OpenFile("mz1.xml", FileMode.Open);
                using (StreamReader reader = new StreamReader(isoFileStream))
                {
                    textBlock1.Text= reader.ReadToEnd();
                }
            }

Output:

     <?xml version="1.0" encoding="utf-8"?>
like image 394
Mj1992 Avatar asked Mar 24 '12 10:03

Mj1992


2 Answers

In response to your first question, you can download and install the WP7 Isolated Storage Explorer from codeplex here: http://wp7explorer.codeplex.com/

Its really easy to use. Just add a couple lines of code to your app.xaml.cs and you're all set.

In regard to your second question, The code that you have there looks OK. I recently wrote a little WP7 app that did just this kind of thing as well. Here is some of that code:

public List<Task> GetTasks()
{
    var tasks = new List<Task>();
    using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
    {
        if (store.FileExists(XmlFile))
        {
            //store.DeleteFile(XmlFile);
            //XDocument doc = XDocument.Load(store.OpenFile(XmlFile, FileMode.Open));
            using (var sr = new StreamReader(new IsolatedStorageFileStream(XmlFile, FileMode.Open, store)))
            {
                XDocument doc = XDocument.Load(sr);
                tasks = (from d in doc.Descendants("task")
                         select new Task
                                    {
                                        Category = (string) d.Attribute("category"),
                                        Id = (string) d.Attribute("id"),
                                        Name = (string) d.Element("name"),
                                        CreateDate = (DateTime) d.Element("createdate"),
                                        DueDate = (DateTime) d.Element("duedate"),
                                        IsComplete = (bool) d.Element("isComplete")
                                    }).ToList<Task>();
            }
        }
    }
    return tasks;
}

its up to you, but you may want to consider using LinqToXml. It makes things a bit cleaner IMHO.

I actually have a blog post that does all of this posted here:

http://www.ritzcovan.com/2012/02/building-a-simple-windows-phone-apppart-2/

and you can download all the code as well. I hope you find it helpful.

like image 196
Alex Avatar answered Oct 29 '22 09:10

Alex


Your code executes and works fine. I've changed the result to be set not in TextBlock but to string variable, and it outputs the following:

<?xml version="1.0" encoding="utf-8"?>
<person>
  <node1>value1</node1>
</person>

I guess the TextBlock just shows the first line of the results.

like image 32
Dmitry Reznik Avatar answered Oct 29 '22 08:10

Dmitry Reznik