How to: Load XHTML with a DTD Reference Using XmlPreloadedResolver

Silverlight

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):

    
    using System.Xml.Resolvers;
    using System.Xml;
    using System.IO;
    using System.Text;
    using System.Xml.Linq;
    
    
    

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.



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();
}


Community Additions

ADD
Show: