Sdílet prostřednictvím


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

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

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.

    <bookstore>
        <book genre='novel' ISBN='10-861003-324'>
            <title>The Handmaid's Tale</title>
            <price>19.95</price>
        </book>
        <book genre='novel' ISBN='1-861001-57-5'>
            <title>Pride And Prejudice</title>
            <price>24.95</price>
        </book>
    </bookstore>
    
  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):

    Imports System.Xml.Linq
    Imports System.Xml
    Imports System.Text
    
    using System.Xml;
    using System.Text;
    using System.Xml.Linq;
    

Example

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.

Dim output As StringBuilder = New StringBuilder()

' XmlXapResolver is the default resolver. 
Using reader As XmlReader = XmlReader.Create("book.xml")

    ' Moves the reader to the root element.
    reader.MoveToContent()

    reader.ReadToFollowing("book")
    ' 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...")
    output.AppendLine(reader.ReadInnerXml())

    reader.ReadToFollowing("book")

    ' 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...")
    output.AppendLine(reader.ReadOuterXml())
End Using

OutputTextBlock.Text = output.ToString()
StringBuilder output = new StringBuilder();

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

    reader.ReadToFollowing("book");
    // 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...");
    output.AppendLine(reader.ReadInnerXml());

    reader.ReadToFollowing("book");

    // 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...");
    output.AppendLine(reader.ReadOuterXml());

}

OutputTextBlock.Text = output.ToString();