共用方式為


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

發生執行階段錯誤 (例如中斷的伺服器連線)。

範例

下列範例示範 GetEntityResolveUri 方法。

#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 方法的情況。

適用於

另請參閱