XmlResolver 类

定义

解析由统一资源标识符 (URI) 命名的外部 XML 资源。Resolves external XML resources named by a Uniform Resource Identifier (URI).

public ref class XmlResolver abstract
public abstract class XmlResolver
type XmlResolver = class
Public MustInherit Class XmlResolver
继承
XmlResolver
派生

示例

下面的示例 XmlUrlResolver 使用默认凭据创建一个。The following example creates an XmlUrlResolver with default credentials. XmlReader用于读取和显示生成的数据流。A XmlReader is used to read and display the resulting data stream.

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

class Example
{
    static void Main()
    {
        // Create an XmlUrlResolver with default credentials.
        XmlUrlResolver resolver = new XmlUrlResolver();
        resolver.Credentials = System.Net.CredentialCache.DefaultCredentials;

        // Point the resolver at the desired resource and resolve as a stream.
        Uri baseUri = new Uri("http://serverName/");
        Uri fulluri = resolver.ResolveUri(baseUri, "fileName.xml");
        Stream s = (Stream)resolver.GetEntity(fulluri, null, typeof(Stream));

        // Create the reader with the resolved stream and display the data.
        XmlReader reader = XmlReader.Create(s);
        while (reader.Read())
        {
            Console.WriteLine(reader.ReadOuterXml());
        }
    }
}
Imports System.Xml
Imports System.IO

Module Module1

    Sub Main()
        ' Create an XmlUrlResolver with default credentials.
        Dim resolver As New XmlUrlResolver()
        resolver.Credentials = System.Net.CredentialCache.DefaultCredentials

        ' Point the resolver at the desired resource and resolve as a stream.
        Dim baseUri As New Uri("http://serverName/")
        Dim fulluri As Uri = resolver.ResolveUri(baseUri, "fileName.xml")
        Dim s As Stream = CType(resolver.GetEntity(fulluri, Nothing, GetType(Stream)), Stream)

        ' Create the reader with the resolved stream and display the data.
        Dim reader As XmlReader = XmlReader.Create(s)
        While reader.Read()
            Console.WriteLine(reader.ReadOuterXml())
        End While
    End Sub
End Module

注解

XmlResolver类型用于解析外部 XML 资源,如实体、文档类型定义 (dtd) 或架构。The XmlResolver type is used to resolve external XML resources, such as entities, document type definitions (DTDs), or schemas. 它还用于处理可扩展样式表语言 (XSL) 样式表或 XML 架构定义语言 (XSD) 架构中找到的包含和导入元素。It is also used to process include and import elements found in Extensible Stylesheet Language (XSL) style sheets or XML Schema definition language (XSD) schemas.

XmlResolver 处理与资源协商连接的所有方面,包括处理安全凭据、打开与数据源的连接,并以流或其他对象类型的形式返回资源。XmlResolver handles all aspects of negotiating the connection to the resources, including handling security credentials, opening the connection to the data source, and returning the resource in the form of a stream or other object type. 调用的对象 XmlResolver 具有解释流的任务。The object that calls XmlResolver has the task of interpreting the stream.

System.Xml命名空间包括类的两个具体实现 XmlResolverThe System.Xml namespace includes two concrete implementations of the XmlResolver class:

你可以创建并指定自己的解析程序。You can create and specify your own resolver. 如果未指定解析程序,则读取器将使用 XmlUrlResolver 没有用户凭据的默认值。If you don't specify a resolver, the reader uses a default XmlUrlResolver with no user credentials.

通过设置 XmlResolver 属性并将 XmlReaderSettings.XmlResolver 对象传递给 XmlReaderSettings 方法,可以指定要使用的 CreateYou specify the XmlResolver to use by setting the XmlReaderSettings.XmlResolver property and passing the XmlReaderSettings object to the Create method.

如果资源存储在要求身份验证的系统上,则使用 XmlResolver.Credentials 属性指定必要的凭据。If the resource is stored on a system that requires authentication, you use the XmlResolver.Credentials property to specify the necessary credentials.

提供身份验证凭据Supplying authentication credentials

包含要读取的 XML 数据的文件可能具有受限访问策略。The file that contains the XML data to read may have a restricted access policy. 如果需要身份验证才能访问网络资源,请使用 Credentials 属性指定必要的凭据。If authentication is required to access a network resource, use the Credentials property to specify the necessary credentials. 如果 Credentials 未设置该属性,则凭据设置为 nullIf the Credentials property is not set, credentials are set to null.

例如,假定在出于身份验证目的从 web 请求数据时需要凭据。For example, assume that credentials are needed when requesting data from the web for authentication purposes. 如果 web 虚拟目录允许匿名访问,则必须将属性设置 Credentials 为提供凭据。Unless the web virtual directory allows anonymous access, you must set the Credentials property to supply credentials. 下面的示例创建一个 XmlReader 对象,该对象使用 XmlUrlResolver 具有默认凭据的来访问 http://localhost/bookstore/inventory.xml 站点。The following example creates an XmlReader object that uses an XmlUrlResolver with default credentials to access the http://localhost/bookstore/inventory.xml site.

// Create a resolver with default credentials.
XmlUrlResolver^ resolver = gcnew XmlUrlResolver;
resolver->Credentials = System::Net::CredentialCache::DefaultCredentials;

 // Set the reader settings object to use the resolver.
 settings->XmlResolver = resolver;

// Create the XmlReader object.
XmlReader^ reader = XmlReader::Create( L"http://ServerName/data/books.xml", settings );

// Create a resolver with default credentials.
XmlUrlResolver resolver = new XmlUrlResolver();
resolver.Credentials = System.Net.CredentialCache.DefaultCredentials;

// Set the reader settings object to use the resolver.
settings.XmlResolver = resolver;

// Create the XmlReader object.
XmlReader reader = XmlReader.Create("http://ServerName/data/books.xml", settings);
' Create a resolver with default credentials.
Dim resolver as XmlUrlResolver = new XmlUrlResolver()
resolver.Credentials = System.Net.CredentialCache.DefaultCredentials

' Set the reader settings object to use the resolver.
settings.XmlResolver = resolver

' Create the XmlReader object.
Dim reader as XmlReader = XmlReader.Create("http://ServerName/data/books.xml", settings)

可以为不同的 Uri 提供不同的凭据,并将其添加到缓存中。You can supply different credentials for different URIs and add them to a cache. 这些凭据用于检查不同 URI 的身份验证,与 XML 的原始源无关。These credentials are used to check authentication for the different URIs regardless of the original source of the XML. 下面的示例演示如何将凭据添加到缓存中。The following example shows how to add credentials to a cache.

// Create the credentials.
NetworkCredential myCred = new NetworkCredential(UserName,SecurelyStoredPassword,Domain);
CredentialCache myCache = new CredentialCache();
myCache.Add(new Uri("http://www.contoso.com/"), "Basic", myCred);
myCache.Add(new Uri("http://app.contoso.com/"), "Basic", myCred);

// Set the credentials on the XmlUrlResolver object.
XmlUrlResolver resolver = new XmlUrlResolver();
resolver.Credentials = myCache;

// Compile the style sheet.
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load("http://serverName/data/xsl/order.xsl",XsltSettings.Default, resolver);	
' Create the credentials.
Dim myCred As NetworkCredential = New NetworkCredential(UserName,SecurelyStoredPassword,Domain)
Dim myCache As CredentialCache = New CredentialCache()
myCache.Add(new Uri("http://www.contoso.com/"), "Basic", myCred)
myCache.Add(new Uri("http://app.contoso.com/"), "Basic", myCred)

' Set the credentials on the XmlUrlResolver object.
Dim resolver As XmlUrlResolver = New XmlUrlResolver()
resolver.Credentials = myCache

' Compile the style sheet.
Dim xslt As XslCompiledTransform = New XslCompiledTransform()
xslt.Load("http://serverName/data/xsl/order.xsl", XsltSettings.Default, resolver)

安全注意事项Security considerations

使用类时,请考虑以下各项 XmlResolverConsider the following items when working with the XmlResolver class.

  • XmlResolver 对象可以包含敏感信息,例如用户凭据。XmlResolver objects can contain sensitive information such as user credentials. 在缓存对象时应谨慎 XmlResolver ,并且不应将 XmlResolver 对象传递给不受信任的组件。You should be careful when caching XmlResolver objects and should not pass the XmlResolver object to an untrusted component.

  • 如果您设计的类属性使用 XmlResolver 类,该属性应定义为只写属性。If you are designing a class property that uses the XmlResolver class, the property should be defined as a write-only property. 该属性可以用于指定要使用的 XmlResolver,但是不能用于返回 XmlResolver 对象。The property can be used to specify the XmlResolver to use, but it cannot be used to return an XmlResolver object.

  • 如果你的应用程序接受 XmlResolver 来自不受信任的代码的对象,则无法假定传入方法的 URI 与 GetEntity 方法返回的 URI 相同 ResolveUriIf your application accepts XmlResolver objects from untrusted code, you cannot assume that the URI passed into the GetEntity method will be the same as that returned by the ResolveUri method. XmlResolver 类派生的类可以重写 GetEntity 方法,并且返回的数据可以不同于原始 URI 中包含的数据。Classes derived from the XmlResolver class can override the GetEntity method and return data that is different than what was contained in the original URI.

  • 应用程序可以 GetEntity 通过实现一个 IStream 限制读取的字节数的来缓解对方法的内存拒绝服务威胁。Your application can mitigate memory denial of service threats to the GetEntity method by implementing an IStream that limits the number of bytes read. 这有助于防止恶意代码尝试将无限字节流传递到方法的情况 GetEntityThis helps guard against situations where malicious code attempts to pass an infinite stream of bytes to the GetEntity method.

构造函数

XmlResolver()

初始化 XmlResolver 类的新实例。Initializes a new instance of the XmlResolver class.

属性

Credentials

当在派生类中被重写时,设置用于对 Web 请求进行身份验证的凭据。When overridden in a derived class, sets the credentials used to authenticate web requests.

方法

Equals(Object)

确定指定对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetEntity(Uri, String, Type)

当在派生类中被重写时,将 URI 映射到包含实际资源的对象。When overridden in a derived class, maps a URI to an object that contains the actual resource.

GetEntityAsync(Uri, String, Type)

将 URI 异步映射到包含实际资源的对象。Asynchronously maps a URI to an object that contains the actual resource.

GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
ResolveUri(Uri, String)

当在派生类中被重写时,从基 URI 和相对 URI 解析绝对 URI。When overridden in a derived class, resolves the absolute URI from the base and relative URIs.

SupportsType(Uri, Type)

使解决程序能够返回 Stream 以外的类型。Enables the resolver to return types other than Stream.

ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(继承自 Object)

适用于

另请参阅