How to: Save to and Load from Isolated Storage with LINQ to XML
In Silverlight you can load and save files by using classes defined in the System.IO.IsolatedStorage namespace. Other ways of loading files are:
-
By using XmlXapResolver to load files that are in the application's XAP package. For more information, see Working with XmlXapResolver.
-
By using XmlPreloadedResolver to load from pre-populate cache. For more information, see Working with XmlPreloadedResolver.
-
By using the asynchronous WebRequest to load files from URI locations. For more information, see How to: Load an XML File from an Arbitrary URI Location with LINQ to XML.
To configure a Silverlight Visual Studio project to run this example
-
Modify your page.xaml file so that it includes the following TextBlock element:
<TextBlock x:Name ="OutputTextBlock" Canvas.Top ="10" TextWrapping="Wrap"/>
-
In the page.xaml.cs (page.xaml.vb in Visual Basic) source file for your application, add the following using statements (Imports in Visual Basic):
The following example obtains the isolated storage for the user. It then saves the file to an application's isolated storage and later loads the file from an isolated storage.
XDocument doc = new XDocument( new XComment("This is a comment"), new XElement("Root", new XElement("Child1", "data1"), new XElement("Child2", "data2") ) ); using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication()) { using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream("myFile.xml", FileMode.Create, isoStore)) { doc.Save(isoStream); } } using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication()) { using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream("myFile.xml", FileMode.Open, isoStore)) { XDocument doc1 = XDocument.Load(isoStream); OutputTextBlock.Text = doc1.ToString(); } }