How to: Load a File from a XAP Package Using XmlXapResolver


This topic shows how to use XmlXapResolver to load a file from an application's XAP package.

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. Create the book.xml file, and add it to your project. This will also add it to your applications XAP package. Make sure that the Build Action property of the book.xml file is set to Content.

        <book genre='novel' ISBN='10-861003-324'>
            <title>The Handmaid's Tale</title>
        <book genre='novel' ISBN='1-861001-57-5'>
            <title>Pride And Prejudice</title>
  3. 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.Xml;
    using System.Text;
    using System.Xml.Linq;

The following example shows how to load an XML file from your application's XAP file. It then uses the ReadInnerXml and ReadOuterXml methods to read element content.

StringBuilder output = new StringBuilder();

// XmlXapResolver is the default resolver.
using (XmlReader reader = XmlReader.Create("book.xml"))
    // Moves the reader to the root element.

    // Note that ReadInnerXml only returns the markup of the node's children
    // so the book's attributes are not returned.
    output.AppendLine("Read the first book using ReadInnerXml...");


    // ReadOuterXml returns the markup for the current node and its children
    // so the book's attributes are also returned.
    output.AppendLine("Read the second book using ReadOuterXml...");


OutputTextBlock.Text = output.ToString();

Community Additions