XmlResolver Class

Represents a base class for resolving XML resources. This class is abstract and must be inherited.

Namespace: System.Xml

Assembly: System.Xml.dll

Description

The XmlResolver class provides a mechanism to resolve external resources that are referenced by XML documents, such as DTDs, schemas, and entities. It defines a standard interface for looking up and retrieving these resources, allowing for customization of the resolution process.

Syntax

public abstract class XmlResolver

Methods

Remarks

The XmlResolver class is fundamental for handling external entities in XML processing. When an XML parser encounters a reference to an external resource (like a <!DOCTYPE ... PUBLIC "..." "..."> declaration or an <!ENTITY ... SYSTEM "..."> declaration), it uses the current XmlResolver to locate and retrieve that resource.

The .NET Framework provides a default implementation, XmlUrlResolver, which resolves URIs using standard HTTP, HTTPS, FTP, and file protocols. You can create custom implementations of XmlResolver to provide different resolution logic, such as accessing resources from a database, a custom file system, or a network share, or to implement security checks before resolving resources.

To use a custom resolver, you typically set the XmlResolver property of an XmlReaderSettings object before creating the XmlReader.

Examples

The following example demonstrates how to use the default XmlUrlResolver to resolve external entities.


using System;
using System.Xml;
using System.IO;

public class XmlResolverExample
{
    public static void Main(string[] args)
    {
        string xmlString = @"
                            
                            
                                Hello, World!
                            ";

        // Create a dummy DTD file for demonstration
        string dtdContent = @"
                              ";
        File.WriteAllText("root.dtd", dtdContent);

        try
        {
            XmlReaderSettings settings = new XmlReaderSettings();
            // Use the default XmlUrlResolver
            settings.XmlResolver = new XmlUrlResolver();
            settings.DtdProcessing = DtdProcessing.Parse; // Important for DTD resolution

            using (StringReader stringReader = new StringReader(xmlString))
            using (XmlReader reader = XmlReader.Create(stringReader, settings))
            {
                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Element && reader.Name == "message")
                    {
                        Console.WriteLine($"Message: {reader.ReadElementContentAsString()}");
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"An error occurred: {ex.Message}");
        }
        finally
        {
            // Clean up the dummy DTD file
            if (File.Exists("root.dtd"))
            {
                File.Delete("root.dtd");
            }
        }
    }
}