Introducing the XmlPreloadedResolver

The XmlPreloadedResolver is a new type that we’ve been working on in SilverLight that provides the ability to load a DTD without a call to the network. The new type can act as a manually maintainable cache of DTD’s and XML Streams.

Let’s look at two examples of how the XmlPreloadedResolver can be used.

Example 1:

The XmlPreloadedResolver can be used to pre-load an external DTD. In the following example, a user-defined DTD is pre-loaded into the resolver. The resolver is then used when loading the XML file.

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

String strXml = @"<!DOCTYPE doc SYSTEM 'myDtd.dtd'>

<doc>&entity;</doc>";

System.Xml.Resolvers.XmlPreloadedResolver resolver = new XmlPreloadedResolver();

resolver.Add(resolver.ResolveUri(null, "myDtd.dtd"), strDtd);

XmlReaderSettings rs = new XmlReaderSettings();

rs.XmlResolver = resolver;

rs.DtdProcessing = DtdProcessing.Parse;

XmlReader reader = XmlReader.Create(new StringReader(strXml), rs);

For the complete example, refer to: https://msdn.microsoft.com/en-us/library/cc189063(VS.95).aspx

Example 2:

The XmlPreloadedResolver also contains two well-known DTD sets: XHTML 1.0 and RSS 0.91. These two well known DTD sets can be enabled via the XmlKnownDtd enumeration. The following is an example of using the XmlPreloadedResolver to preload the XHTML 1.0 DTD into an XmlReaderSettings object. In this example, no network calls are needed to parse an XHTML file.

XmlReaderSettings settings = new XmlReaderSettings();

settings.DtdProcessing = DtdProcessing.Parse;

settings.XmlResolver =

new XmlPreloadedResolver(XmlKnownDtds.Xhtml10);

XmlReader reader = XmlReader.Create("HTMLPage.html", settings);

XDocument document = XDocument.Load(reader);

For the complete example, refer to: https://msdn.microsoft.com/en-us/library/cc189059(VS.95).aspx

Look for this new class in System.Xml.Resolvers namespace in a new DLL: System.Xml.Utils.dll that will be included as part of the upcoming SilverLight 2.0 SDK.

Shayne Burgess
Program Manager | Data Programmability

- and -

Helena Kotas
Senior Software Development Engineer | Data Programmability