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"?>
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With