Export (0) Print
Expand All

How to: Preload a User-Defined DTD Using XmlPreloadedResolver

Silverlight

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="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://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):

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

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.


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.


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


Community Additions

ADD
Show:
© 2014 Microsoft