Udostępnij za pośrednictwem


How to: Save XMLWriter Content to Isolated Storage

Microsoft Silverlight will reach end of support after October 2021. Learn more.

This topic shows how to use the Microsoft .NET Framework for Silverlight to save XmlWriter content to an isolated storage file.

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):

    Imports System.Xml
    Imports System.IO
    Imports System.Text
    Imports System.IO.IsolatedStorage
    
    using System.Xml;
    using System.IO;
    using System.Text;
    using System.IO.IsolatedStorage;
    

Example

The code example does the following:

  • Obtains the isolated storage for the user.

  • Creates a new file in the isolated storage.

  • Uses the stream writer and an XmlWriterSettings object to create a new XmlWriter instance.

  • Writes content to the XmlWriter and calls the Flush method so that the content of XmlWriter content is written to the isolated storage.

  • Uses StreamReader to read the content of the file, and then writes the content to the page.

  • Deletes the file.

Using isoStore As IsolatedStorageFile = _
    IsolatedStorageFile.GetUserStoreForApplication()

    ' Create new file
    Using isoStream As IsolatedStorageFileStream = _
        New IsolatedStorageFileStream("IsoStoreFile.xml", _
            FileMode.Create, isoStore)

        ' Write to the Isolated Storage for the user.
        Dim settings As XmlWriterSettings = New XmlWriterSettings()
        settings.Indent = True
        ' Create an XmlWriter.
        Using writer As XmlWriter = XmlWriter.Create(isoStream, settings)
            writer.WriteComment("sample XML document")

            ' Write an element (this one is the root).
            writer.WriteStartElement("book")

            ' Write the namespace declaration.
            writer.WriteAttributeString("xmlns", "bk", Nothing, "urn:samples")

            ' Write the genre attribute.
            writer.WriteAttributeString("genre", "novel")

            ' Write the title.
            writer.WriteStartElement("title")
            writer.WriteString("The Handmaid's Tale")
            writer.WriteEndElement()

            ' Write the price.
            writer.WriteElementString("price", "19.95")

            writer.WriteStartElement(Nothing, "ISBN", "urn:samples")
            writer.WriteString("1-861003-78")
            writer.WriteEndElement()

            ' Write the style element (shows a different way to handle prefixes).
            writer.WriteElementString("style", "urn:samples", "hardcover")

            ' Write the close tag for the root element.
            writer.WriteEndElement()

            ' Write the XML to the file.
            writer.Flush()
        End Using
    End Using

    ' Open the file again for reading.
    Using reader As New StreamReader(isoStore.OpenFile("IsoStoreFile.xml", FileMode.Open))
        OutputTextBlock.Text = reader.ReadToEnd()
    End Using

    ' Delete the IsoStoreFile.xml file.
    isoStore.DeleteFile("IsoStoreFile.xml")
using (IsolatedStorageFile isoStore =
    IsolatedStorageFile.GetUserStoreForApplication())
{

    // Create new file
    using (IsolatedStorageFileStream isoStream =
        new IsolatedStorageFileStream("IsoStoreFile.xml",
            FileMode.Create, isoStore))
    {
        // Write to the Isolated Storage for the user.
        XmlWriterSettings settings = new XmlWriterSettings();
        settings.Indent = true;
        // Create an XmlWriter.
        using (XmlWriter writer = XmlWriter.Create(isoStream, settings))
        {

            writer.WriteComment("sample XML document");

            // Write an element (this one is the root).
            writer.WriteStartElement("book");

            // Write the namespace declaration.
            writer.WriteAttributeString("xmlns", "bk", null, "urn:samples");

            // Write the genre attribute.
            writer.WriteAttributeString("genre", "novel");

            // Write the title.
            writer.WriteStartElement("title");
            writer.WriteString("The Handmaid's Tale");
            writer.WriteEndElement();

            // Write the price.
            writer.WriteElementString("price", "19.95");

            writer.WriteStartElement(null, "ISBN", "urn:samples");
            writer.WriteString("1-861003-78");
            writer.WriteEndElement();

            // Write the style element (shows a different way to handle prefixes).
            writer.WriteElementString("style", "urn:samples", "hardcover");

            // Write the close tag for the root element.
            writer.WriteEndElement();

            // Write the XML to the file.
            writer.Flush();
        }
    }
    // Open the file again for reading.
    using (StreamReader reader =
                    new StreamReader(isoStore.OpenFile("IsoStoreFile.xml", FileMode.Open)))
    {
        OutputTextBlock.Text = reader.ReadToEnd();
    }


    // Delete the IsoStoreFile.xml file.
    isoStore.DeleteFile("IsoStoreFile.xml");
}

See Also

Other Resources