How to: Load XHTML with a DTD Reference Using XmlPreloadedResolver

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

This topic shows how to use XmlPreloadedResolver to preload the XHTML 1.0 DTD that is referenced in an XHML file.

To configure a Silverlight Visual Studio project to run this example

  1. In Solution Explorer, add an assembly reference to the System.Xml.Utils.dll.

  2. Modify your page.xaml file so that it includes the following TextBlock element:

    <TextBlock x:Name ="OutputTextBlock" Canvas.Top ="10" TextWrapping="Wrap"/>
    
  3. Create the XHTMLPage.html file that is used in the example, and then 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 XHTMLPage.html file is set to Content.

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
            "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title> XHTML Example </title>
    </head>
    </html>
    
  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 loads an HTML page that references XHTML DTDs by setting XmlKnownDtd.Xhtml10 on the XmlPreloadedResolver. In this example, the XHTMLPage.html file is a part of the application's XAP package. Therefore, we have to pass a fallback resolver to the constructor of the XmlPreloadedResolver.

Dim settings As XmlReaderSettings = New XmlReaderSettings()
settings.XmlResolver = New XmlPreloadedResolver(New XmlXapResolver(), XmlKnownDtds.Xhtml10)
Using reader As XmlReader = XmlReader.Create("HTMLPage.html", settings)
    Dim document As XDocument = XDocument.Load(reader)
    OutputTextBlock.Text = document.ToString()
End Using

XmlReaderSettings settings = new XmlReaderSettings();
settings.DtdProcessing = DtdProcessing.Parse;
settings.XmlResolver =
    new XmlPreloadedResolver(new XmlXapResolver(),
        XmlKnownDtds.Xhtml10);

using (XmlReader reader = XmlReader.Create("HTMLPage.html", settings))
{
XDocument document = XDocument.Load(reader);
OutputTextBlock.Text = document.ToString();
}