All about WP7 Isolated Storage - Read and Save XML files using XmlWriter
published on: 3/28/2011 | Views: N/A | Tags: IsoStore
by WindowsPhoneGeek
This is the 6th article from the "All about WP7 Isolated Storage " series of short articles focused on real practical examples with source code rather than a plain theory. I am going to talk about how to read and save XML file into Isolated Storage using XmlWriter.
- All about WP7 Isolated Storage - intro to Isolated Storage
- All about WP7 Isolated Storage - Folders and Files
- All about WP7 Isolated Storage - Store data in IsolatedStorageSettings
- All about WP7 Isolated Storage - Read and Save Text files
- All about WP7 Isolated Storage - Read and Save XML files using XmlSerializer
- All about WP7 Isolated Storage - Read and Save XML files using XmlWriter
- All about WP7 Isolated Storage - Read and Save Images
- All about WP7 Isolated Storage - Read and Save Captured Image
- All about WP7 Isolated Storage - Read and Save Binary files
- All about WP7 Isolated Storage - File manipulations
- All about WP7 Isolated Storage - Recommendations and Best Practices
- All about WP7 Isolated Storage - open source Databases and Helper libraries
To begin with lets first create a sample Windows Phone 7 application project. Next include the following namespaces in MainPage.xaml.cs (alternatively you can use the code in another page):
using System.Xml; using System.IO.IsolatedStorage; using System.IO;
Reading and saving XML files to the Isolated Storage is a common task for many WP7 applications. In my previous post I explained how to write/read XML files using XmlSerializer, you can take a look here for reference: All about WP7 Isolated Storage - Read and Save XML files using XmlSerializer. In this post we will focus on how to do this using XmlWriter.
NOTE: When working with files always use Using statement because it provides a convenient syntax that ensures the correct use of IDisposable objects.
Save New XML File to Isolated Storage using XmlWriter
In this example at first we create a new People2.xml XML file in the Isolated Storage and after that write data into it.
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream("People2.xml", FileMode.Create, myIsolatedStorage))
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
using (XmlWriter writer = XmlWriter.Create(isoStream, settings))
{
writer.WriteStartElement("p", "person", "urn:person");
writer.WriteStartElement("FirstName", "");
writer.WriteString("Kate");
writer.WriteEndElement();
writer.WriteStartElement("LastName", "");
writer.WriteString("Brown");
writer.WriteEndElement();
writer.WriteStartElement("Age", "");
writer.WriteString("25");
writer.WriteEndElement();
// Ends the document
writer.WriteEndDocument();
// Write the XML to the file.
writer.Flush();
}
}
}
Read XML File from Isolated Storage using StreamReader
In this example at first open an existing People2.xml XML file from the Isolated Storage and read its content. After that the content is visualized as a TextBlock Text.
try
{
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
IsolatedStorageFileStream isoFileStream = myIsolatedStorage.OpenFile("People2.xml", FileMode.Open);
using (StreamReader reader = new StreamReader(isoFileStream))
{
this.tbx.Text = reader.ReadToEnd();
}
}
}
catch
{ }
NOTE: When working with files always use Using statement because it provides a convenient syntax that ensures the correct use of IDisposable objects.
In this article I talked about reading and saving XML file into Isolated Storage using XmlWriter. Here is the full source code (including all XmlWriter and XmlSerializer examples from the previous post ):
Stay tuned with the rest of the posts. I hope that the article was helpful.
You can also follow us on Twitter @winphonegeek
Comments
error project
posted by: juan on 4/7/2011 8:27:58 PM
hello the file of the project, can´t compile
RE: @juan
posted by: winphonegeek on 4/10/2011 12:11:37 PM
Can you please share some more info about the problem that you faced? We were unable to reproduce this issue.
Don't work the example...
posted by: Ruben on 2/23/2012 6:37:15 PM
Hi! I tested your example project to look how to write data to xml file in windows phone, but i open the project, compile but the app don't create any file
Our Top Articles & Free books
- Our FREE e-book: "Windows Phone Toolkit In Depth" 2nd edition
- 400+ Windows Phone Development articles in our Article Index
- 21 WP7 Toolkit in Depth articles covering all controls
- 12 WP7 Coding4Fun Toolkit in Depth articles covering all controls
- Performance Tips when creating WP7 apps
- Creating a WP7 Custom Control in 7 Steps
- WP7 working with VisualStates: How to make a ToggleSwitch from CheckBox
- What makes a WP7 App successful
- Creating theme friendly UI in WP7 using OpacityMask
- Implementing Windows Phone 7 DataTemplateSelector and CustomDataTemplateSelector
- All about Splash Screens in WP7 – Creating animated Splash Screen
- Getting Started with Unit Testing in Silverlight for WP7
- WP7 WatermarkedTextBox custom control
Our Top Tips & Samples
- All about WP7 Isolated Storage series
- WP7 Dynamically Generating DataTemplate in code
- 5 tips for a successful WP7 Marketplace submission
- WP7: Navigating to a page in different assembly
- WP7 ContextMenu: answers to popular questions
- WP7 ListBox: answers to popular questions
- WP7 working with Images: Content vs Resource build action
- WP7 Element Binding samples
- WP7 working with XML: reading, filtering and databinding
- Drawing in WP7: #2 Drawing shapes with finger
- WP7 TextBox Light theme problems - the solution
- Changing the WP7 Panorama Background Image dynamically with Animation
