Freigeben über


How to: Implicitly Load a DTD from a XAP Package Using XmlXapResolver

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

The example in this topic uses XmlXapResolver to load an XML file that references an external DTD from the XAP package. This implicitly loads the DTD.

To configure a Silverlight Visual Studio project to run this example

  1. In Solution Explorer, add assembly references to System.Xml.dll.

  2. Create the Nmtoken.dtd file, and add it to your project. This will also add it to your application's XAP package. Make sure that the Build Action property of the Nmtoken.dtd file is set to Content. Add the following content to Nmtoken.dtd:

    <!ELEMENT datatype (desc)>
    <!ELEMENT desc (#PCDATA)>
    <!ENTITY entity 'Replacement text'>
    <!ATTLIST desc
       att NMTOKEN #REQUIRED
    >
    
  3. Create the Nmtoken.xml file, and add it to your project. This will also add it to your application's XAP package. Make sure that the Build Action property of the Nmtoken.xmlfile is set to Content. Add the following content to Nmtoken.xml:

    <?xml version="1.0"?>
    <!DOCTYPE datatype SYSTEM "nmtoken.dtd">
    <datatype>
       <desc att="abc">&entity;</desc>
    </datatype>
    
  4. 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 that references a DTD file from your application's XAP package, using XmlXapResolver.

Dim output As StringBuilder = New StringBuilder()

Dim rs As XmlReaderSettings = New XmlReaderSettings()
rs.DtdProcessing = DtdProcessing.Parse

' XmlXapResolver is the default resolver.
Using reader As XmlReader = XmlReader.Create("nmtoken.xml", rs)
    Dim document As XDocument = XDocument.Load(reader)
    OutputTextBlock.Text = document.ToString()
End Using
StringBuilder output = new StringBuilder();

XmlReaderSettings rs = new XmlReaderSettings();
rs.DtdProcessing = DtdProcessing.Parse;

// XmlXapResolver is the default resolver.
using (XmlReader reader = XmlReader.Create("nmtoken.xml", rs))
{
    XDocument document = XDocument.Load(reader);
    OutputTextBlock.Text = document.ToString();
}