WP7 isolated storage series-6. Use xmlwriter to read and store XML files

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.