This is the sixth short article on the series "WP7 isolated storage series", focusing on real and practical examples with source code, rather than the theory of inventory. Next I will discuss how to read data from isolated storage and save data to isolated storage.
·
WP7 isolated storage series-1. Introduction to isolated storage
·
WP7 isolated storage series-2. Create folders and files
·
WP7 isolated storage series-3. Use isolatedstoragesettings to store data
·
WP7 isolated storage series-4. Read and store text files
·
WP7 isolated storage series-5. Use xmlserializer to read and store XML files
·
WP7 isolated storage series-6. Use xmlwriter to read and store XML files
·
WP7 isolated storage series-7. Reading and storing images
·
WP7 isolated storage series-8. Read and store captured images
·
WP7 isolated storage series-9. Read and store binary files
·
WP7 isolated storage series-10. File Operations
·
WP7 isolated storage series-11. Suggestions and best practices
·
WP7 isolated storage series-12. open source database and help library files
Let's start by creating a simple Windows Phone 7 project. Next, add the following namespaces to mainpage. XAML. CS (or you can use this code on another page ):
using System.Xml;using System.IO.IsolatedStorage;using System.IO;
Reading and saving XML files to isolated storage is very common in WP7 programs. In my previous article, I talked about how to use xmlserializer to write/read XML files. You can refer to the following link for this article: WP7
Isolated storage series-5. Use xmlserializer to read and store XML files. In this article, we will focus on how to use xmlwriter to do this.
Note: The using declaration is always used for file operations, because it provides a convenient syntax to ensure correct use of the idisposable object.
Use xmlwriter to save the New XML file to isolated storage.
In this example, we will create a new XML file named people2.xml to isolated storage, and then write the 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 documentwriter.WriteEndDocument();// Write the XML to the file.writer.Flush();}}}
Use streamreader to read XML file data from isolated storage
In this example, we will first open an existing XML file named people. XML from isolated storage and read the data, and then display the content in the file to a textblock.
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: The using declaration is always used for file operations, because it provides a convenient syntax to ensure correct use of the idisposable object.
In this article, I talked about using xmlwriter to read and save XML files from isolated storage. Some source code here (including all the examples about xmlwriter and xmlserializer mentioned in the previous article ):
Wp7sampleproject35 (2).zip
We look forward to the following articles. I hope this will help you.
Link: http://www.windowsphonegeek.com/tips/All-about-WP7-Isolated-Storage---Read-and-Save-XML-files-using-XmlWriter