System.Xml.XmlUrlResolver.GetEntity 方法

方法描述

将 URI 映射到包含实际资源的对象。

语法定义(C# System.Xml.XmlUrlResolver.GetEntity 方法 的用法)

public override Object GetEntity(
	Uri absoluteUri,
	string role,
	Type ofObjectToReturn
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
absoluteUri System-Uri 从 ResolveUri 返回的 URI
role System-String 当解析 URI 时,当前实现不使用此参数。提供此参数是为了将来扩展用。例如,它可以映射到 xlink:role 并用作其他方案中的实现特定参数。
ofObjectToReturn System-Type 要返回的对象的类型。当前实现只返回 System.IO.Stream 对象。
返回值 System.Object System.IO.Stream 对象;如果指定了流以外的类型,则为 null。

提示和注释

当调用方要将给定的 URI 映射到包含该 URI 表示的实际资源的对象时,使用此方法。

安全说明

通过实现 IStream 来限制读取的字节数,应用程序可以减轻对 GetEntity 方法的内存拒绝服务威胁。 这有助于防止出现恶意代码尝试向 GetEntity 方法传递无限字节流的情况。

System.Xml.XmlUrlResolver.GetEntity 方法例子

下面的示例阐释 GetEntity 和 ResolveUri 方法。

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

 public class Sample
 {

   public static void Main()
   {
       XmlUrlResolver resolver = new XmlUrlResolver();

       Uri baseUri = new Uri ("http://servername/tmp/test.xsl");

       Uri fulluri=resolver.ResolveUri(baseUri, "includefile.xsl");

       // Get a stream object containing the XSL file
       Stream s=(Stream)resolver.GetEntity(fulluri, null, typeof(Stream));

       // Read the stream object displaying the contents of the XSL file
       XmlTextReader reader = new XmlTextReader(s);
       while (reader.Read()) 
       {
          Console.WriteLine(reader.ReadOuterXml());
       } 
   }
}

异常

异常 异常描述
XmlException ofObjectToReturn 既不是 null 也不是 Stream 类型。
UriFormatException 指定的 URI 不是一个绝对 URI。
ArgumentNullException absoluteUri 为 null。
Exception 存在运行时错误(例如中断的服务器连接)。

命名空间

namespace: System.Xml

程序集: System.Xml(在 System.Xml.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。