Dear All,
I want to read from and write to an xml file in wp7.How can i do that?
Thanks in advance.
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Jaganathan BantheswaranPosted Jul 20, 2011, 2:45 AM
Try this,
Person is an Entity.
GeneratePersonData() is a method which returns collection of persons data.
For writing---
XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
xmlWriterSettings.Indent = true;
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile("People.xml", FileMode.Create))
{
XmlSerializer serializer = new XmlSerializer(typeof(List
using (XmlWriter xmlWriter = XmlWriter.Create(stream, xmlWriterSettings))
{
serializer.Serialize(xmlWriter, GeneratePersonData());
}
}
}
For reading------
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
using(IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile("People.xml", FileMode.Open))
{
XmlSerializer serializer = new XmlSerializer(typeof(List
List
this.listBox.ItemsSource = data;
}
}
Jaganathan BantheswaranPosted Jul 21, 2011, 1:17 AM
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
if(myIsolatedStorage.FileExists("People.xml"))
{
using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile("People.xml", FileMode.Append))
{
XmlSerializer serializer = new XmlSerializer(typeof(List));
using (XmlWriter xmlWriter = XmlWriter.Create(stream, xmlWriterSettings))
{
serializer.Serialize(xmlWriter, GeneratePersonData());
}
}
}
}in this, the FileMode.Append will open the file if exists for appending or will create a new one.
maheshPosted Jul 21, 2011, 12:10 AM
Thanks in advance
Jaganathan BantheswaranPosted Jul 20, 2011, 9:12 AM
maheshPosted Jul 20, 2011, 3:20 AM