Condividi tramite


How to: Preload a User-Defined DTD Using XmlPreloadedResolver

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

This topic provides an example of how to use XmlPreloadedResolver to preload a user-defined DTD that is referenced in XML content.

To configure a Silverlight Visual Studio project to run the example in this topic

  1. Modify your page.xaml file so that it has the following content:

    <UserControl x:Class="SilverlightApplication1.Page"
        xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml" 
        Width="400" Height="300">
        <Canvas x:Name="LayoutRoot" Background="White">
    
            <TextBlock Name ="OutputTextBlock" Canvas.Top ="10" TextWrapping="Wrap"/>
        </Canvas>
    </UserControl>
    
  2. In Solution Explorer, add an assembly reference to System.Xml.Utils.dll.

  3. Create the test.xml file, which is used in the second example, 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 test.xml file is set to Content.

    <!DOCTYPE doc SYSTEM 'myDtd'><doc>&entity;</doc>
    
  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
    Imports System.IO
    Imports System.Text
    Imports System.Xml.Resolvers
    Imports System.Xml.Linq
    
    using System.Xml.Resolvers;
    using System.Xml;
    using System.IO;
    using System.Text;
    using System.Xml.Linq;
    

Example

The following example uses the Add overload method to preload a user-defined DTD and an XML file, which uses the DTD, into the XmlPreloadedResolver.

Dim strDtd As String = "<!ENTITY entity 'Replacement text'>"
Dim strXml As String = "<!DOCTYPE doc SYSTEM 'myDtd'><doc>&entity</doc>"

Dim resolver As XmlPreloadedResolver = New XmlPreloadedResolver()
resolver.Add(resolver.ResolveUri(Nothing, "myXml"), strXml)
resolver.Add(resolver.ResolveUri(Nothing, "myDtd"), strDtd)

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

Using reader As XmlReader = XmlReader.Create("myXml", rs)
    Dim document As XDocument = XDocument.Load(reader)
    OutputTextBlock.Text = document.ToString()
End Using
string strDtd = "<!ENTITY entity 'Replacement text'>";
string strXml = "<!DOCTYPE doc SYSTEM 'myDtd'><doc>&entity;</doc>";

XmlPreloadedResolver resolver = new XmlPreloadedResolver();
resolver.Add(resolver.ResolveUri(null, "myXml"), strXml);
resolver.Add(resolver.ResolveUri(null, "myDtd"), strDtd);

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

using (XmlReader reader = XmlReader.Create("myXml", rs))
{
    XDocument document = XDocument.Load(reader);
    OutputTextBlock.Text = document.ToString();
}

The following example preloads a user-defined DTD into the XmlPreloadedResolver. The DTD is referenced in the test.xml file that is a part of the application's XAP package. To load the file from the XAP package, you must pass a fallback resolver to the XmlPreloadedResolver's constructor.

Dim strDtd As String = "<!ENTITY entity 'Replacement text'>"

Dim resolver As XmlPreloadedResolver = _
        New XmlPreloadedResolver(New XmlXapResolver())
resolver.Add(resolver.ResolveUri(Nothing, "myDtd"), strDtd)

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

Using reader As XmlReader = XmlReader.Create("test.xml", rs)
    Dim document As XDocument = XDocument.Load(reader)
    OutputTextBlock.Text = document.ToString()
End Using
string strDtd = "<!ENTITY entity 'Replacement text'>";

XmlPreloadedResolver resolver =
    new XmlPreloadedResolver(new XmlXapResolver());
resolver.Add(resolver.ResolveUri(null, "myDtd"), strDtd);

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

using (XmlReader reader = XmlReader.Create("test.xml", rs))
{
    XDocument document = XDocument.Load(reader);
    OutputTextBlock.Text = document.ToString();
}