WP7 Read Write Xml in IsolatedStorage
Asked Answered
S

3

5

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"?>
Soccer answered 24/3, 2012 at 10:7 Comment(0)
C
6

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.

Cecillececily answered 24/3, 2012 at 12:9 Comment(2)
thnx @alex for the reply.I installed the explorer and in its documentation it says to add reference to IsolatedStorageExplorer assembly but the assembly is not present in my visual studio although i've installed the explorerSoccer
@Soccer - when you open the add reference dialog, just browse to the directory where the library lives and add a reference to the .dll - typically its installed in C:\Program Files\WP7 Isolated Storage Explorer\Library - hthCecillececily
R
2

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.

Rector answered 24/3, 2012 at 12:1 Comment(0)
B
1

are you looking for something like this?

Burnsed answered 24/3, 2012 at 11:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.