1 out of 3 rated this helpful - Rate this topic

How to: Save to and Load from Isolated Storage with LINQ to XML

Silverlight

In Silverlight you can load and save files by using classes defined in the System.IO.IsolatedStorage namespace. Other ways of loading files are:

To configure a Silverlight Visual Studio project to run this example

  1. Modify your page.xaml file so that it includes the following TextBlock element:

    <TextBlock x:Name ="OutputTextBlock" Canvas.Top ="10" TextWrapping="Wrap"/>
    
  2. 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):

    
    using System.IO;
    using System.Xml.Linq;
    using System.IO.IsolatedStorage;
    
    
    

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();
    }
}


Did you find this helpful?
(1500 characters remaining)

Community Additions

ADD
© 2013 Microsoft. All rights reserved.