XmlUrlResolver.GetEntity(Uri, String, Type) 方法

定义

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

public:
 override System::Object ^ GetEntity(Uri ^ absoluteUri, System::String ^ role, Type ^ ofObjectToReturn);
public override object? GetEntity (Uri absoluteUri, string? role, Type? ofObjectToReturn);
public override object GetEntity (Uri absoluteUri, string role, Type ofObjectToReturn);
override this.GetEntity : Uri * string * Type -> obj
Public Overrides Function GetEntity (absoluteUri As Uri, role As String, ofObjectToReturn As Type) As Object

参数

absoluteUri
Uri

ResolveUri(Uri, String) 返回的 URI。

role
String

当前未使用。

ofObjectToReturn
Type

要返回的对象的类型。 当前实现只返回 Stream 对象。

返回

Object

流对象;如果指定了流以外的类型,则为 null

例外

ofObjectToReturn 既不是 null 也不是 Stream 类型。

指定的 URI 不是一个绝对 URI。

absoluteUrinull

存在运行时错误(例如中断的服务器连接)。

示例

以下示例演示了 GetEntity 方法和 ResolveUri 方法。

#using <System.dll>
#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;

int main()
{
   XmlUrlResolver^ resolver = gcnew XmlUrlResolver;
   Uri^ baseUri = gcnew Uri( "http://servername/tmp/test.xsl" );
   Uri^ fulluri = resolver->ResolveUri( baseUri, "includefile.xsl" );

   // Get a stream object containing the XSL file
   Stream^ s = dynamic_cast<Stream^>(resolver->GetEntity( fulluri, nullptr, Stream::typeid ));

   // Read the stream object displaying the contents of the XSL file
   XmlTextReader^ reader = gcnew XmlTextReader( s );
   while ( reader->Read() )
   {
      Console::WriteLine( reader->ReadOuterXml() );
   }
}
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());
       }
   }
}
Imports System.IO
Imports System.Xml

Public Class Sample
    
  Public Shared Sub Main()

     Dim resolver As New XmlUrlResolver()
     Dim baseUri As New Uri("http://servername/tmp/test.xsl")
     Dim fulluri As Uri = resolver.ResolveUri(baseUri, "includefile.xsl")
            
     ' Get a stream object containing the XSL file
     Dim s As Stream = CType(resolver.GetEntity(fulluri, Nothing, GetType(Stream)), Stream)
            
     ' Read the stream object displaying the contents of the XSL file
     Dim reader As New XmlTextReader(s)
     While reader.Read()
        Console.WriteLine(reader.ReadOuterXml())
     End While
        
  End Sub
End Class

注解

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

有关此方法的异步版本,请参阅 GetEntityAsync

重要

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

适用于

另请参阅