Represents a base class for resolving XML resources. This class is abstract and must be inherited.
Namespace: System.Xml
Assembly: System.Xml.dll
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.
public abstract class XmlResolver
Retrieves an object that can be used to access the content of a resource.
public abstract object GetEntity(Uri absoluteUri, string role, Type requiredObjectType)
Parameters:
absoluteUri
: The URI of the resource to resolve.role
: The role of the resource, if any.requiredObjectType
: The type of object to return. This is typically System.IO.Stream
or System.Xml.XmlReader
.Returns: An object that provides access to the resource's content.
Exceptions:
XmlException
: If the URI cannot be resolved.XmlSchemaException
: If a schema validation error occurs.ArgumentNullException
: If absoluteUri
is null.Resolves the given relative URI against the given base URI.
public abstract Uri ResolveUri(Uri baseUri, string relativeUri)
Parameters:
baseUri
: The base URI to resolve the relative URI against.relativeUri
: The relative URI to resolve.Returns: A Uri
object representing the resolved URI.
Exceptions:
ArgumentNullException
: If relativeUri
is null.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
.
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");
}
}
}
}