解析外部資源Resolving External Resources

XmlDocument 類別可以使用 XmlDocumentXmlResolver 屬性尋找沒有內嵌到 XML 資料的資源,例如外部文件類型定義 (DTD)、實體和結構描述。The XmlResolver property of the XmlDocument is used by the XmlDocument class to locate resources that are not inline in the XML data, such as external document type definitions (DTDs), entities, and schemas. 這些項目可位於網路或本機磁碟上,並且可以由統一資源識別元 (URI) 辨識。These items can be located on a network or on a local drive, and are identifiable by a Uniform Resource Identifier (URI). 如此可讓 XmlDocument 解析文件中出現的 EntityReference 節點,並依據外部 DTD 或結構描述驗證文件。This allows the XmlDocument to resolve EntityReference nodes that are present in the document and validate the document according to the external DTD or schema.

完全信任的 XmlDocumentFully-Trusted XmlDocument

XmlResolver 屬性會影響 XmlDocument.Load 方法的功能。The XmlResolver property affects the functionality of the XmlDocument.Load method. 下表顯示,在 XmlDocument 物件是完全受信任時,XmlDocument.XmlResolver 屬性將會如何運作。The table below shows how the XmlDocument.XmlResolver property works when the XmlDocument object is fully trusted. 下表說明當 Load 的輸入為 TextReaderStringStreamURI 時的 XmlDocument.Load 方法。The following table shows the XmlDocument.Load methods when the input to the Load is a TextReader, String, Stream, or URI. 如果 XmlDocument 是從 XmlReader 載入,則這個表格不適用於 Load 方法。This table does not apply to the Load method if the XmlDocument is loaded from an XmlReader.

XmlResolver 屬性XmlResolver property 功能Function 注意Notes
屬性設定為之前已建立的 XmlResolver 類別,而這個類別已經有由使用者設定好的屬性。The property is set to an XmlResolver class that has been previously created and has properties already set on it by the user. XmlDocument 使用的 XmlResolver,是指定用來解析檔案名稱、解析外部資源的參考 (例如 DTD、實體和結構描述)。The XmlDocument uses the XmlResolver that is given to resolve file names, to resolve references to external resources such as DTDs, entities, and schemas.

當解析在加入或編輯 XmlDocument 中節點必要的外部資源時,也會使用 XmlResolverThe XmlResolver is also used when resolving external resources that are needed when adding or editing nodes in the XmlDocument.
每當您要尋找並解析外部資源時,就必須使用指定給 XmlDocumentXmlResolver 解析程式。The XmlResolver given to the XmlDocument is the resolver that is used whenever external resources need to be located and resolved.
屬性會設定為 null (在 Microsoft Visual Basic .NET中則為 Nothing)。The property is set to null (Nothing in Microsoft Visual Basic .NET). 不支援需要外部資源的功能,例如尋找外部結構描述或 DTD。Features that require an external resource are not supported, such as locating an external schema, or DTD. 也不會解析外部實體,並且也不支援執行編輯功能 (例如插入需要解析的實體節點)。Nor will external entities be resolved, and performing editing functions, such as inserting entity nodes that require resolution, are not supported. XmlDocument 將檔案以匿名載入,並且不會嘗試解析任何其他的資源。The XmlDocument loads files as anonymous and does not attempt to resolve any other resources.
尚未設定屬性,但是保留為預設狀態。The property is not set, but left in its default state. 在解析檔案名稱、尋找外部 DTD、實體和結構描述時,帶有 NULL 認證的 XmlUrlResolver 會被具現化,並且被 XmlDocument 使用,同時在編輯節點時會使用 null 認證。An XmlUrlResolver with NULL credentials will be instantiated and used by the XmlDocument when resolving file names, locating external DTDs, entities, and schemas, and null credentials are used when editing nodes.

下表說明當 Load 的輸入是 XmlReader,且 XmlDocument 是完全受信任時的 XmlDocument.Load 方法。The following table shows the XmlDocument.Load method when the input to the Load is an XmlReader and the XmlDocument is fully trusted.

XmlResolver 屬性XmlResolver property 功能Function 注意Notes
XmlDocument 使用的 XmlResolver 類別與 XmlReader 使用的類別相同。The XmlResolver class used by the XmlDocument is the same class being used by the XmlReader. XmlDocument 使用指定給 XmlReaderXmlResolverThe XmlDocument uses the XmlResolver that was assigned to the XmlReader.

因為 XmlDocument.Resolver 是從 XmlReader 取得 XmlResolver,所以不論 XmlDocument 的信任層級為何,其屬性都無法設定。The XmlDocument.Resolver property cannot be set, regardless of the XmlDocument trust level, because it is getting an XmlResolver from the XmlReader. 您無法藉由設定 XmlDocumentXmlResolver 屬性,來覆寫 XmlReaderXmlResolver 的設定。You cannot attempt to override the settings of the XmlReaders' XmlResolver by setting the XmlResolver property of the XmlDocument.
XmlReader 可能是 XmlTextReaderXmlValidatingReader 或自訂撰寫的讀取器。The XmlReader can be the XmlTextReader, XmlValidatingReader, or a custom-written reader. 如果所用的讀取器支援實體解析,則會解析外部實體。If the reader used supports entity resolution, external entities are resolved. 如果傳遞的讀取器不支援實體參考,則不會解析實體參考。If the reader passed does not support entity references, then entity references are not resolved.

非完全信任的 XmlDocumentSemi-Trusted XmlDocument

下表顯示當物件是非完全信任時,XmlDocument.XmlResolver 屬性如何運作。The following table shows how the XmlDocument.XmlResolver property works when the object is semi-trusted. 當 Load 的輸入為 TextReaderStringStreamURI 時,此表格即適用於 XmlDocument.Load 方法。This table applies to the XmlDocument.Load methods when the input to the Load is a TextReader, String, Stream, or URI. 如果 XmlDocument 是從 XmlReader 載入,則這個表格不適用於 Load 方法。This table does not apply to the Load method if the XmlDocument is loaded from an XmlReader.

XmlResolver 屬性XmlResolver property 功能Function 注意Notes
在非完全信任的案例中,XmlResolver 屬性只能設定為 Null。In the semi-trusted scenario, the XmlResolver property cannot be set to anything other than null. 在解析檔案名稱、尋找外部 DTD、實體和結構描述時,帶有 null 認證的 XmlUrlResolver 會被具現化,並且被 XmlDocument 使用,同時在編輯節點時會使用 null 認證。An XmlUrlResolver with null credentials will be instantiated and used by the XmlDocument when resolving file names, locating external DTDs, entities, and schemas, and null credentials are used when editing nodes. 這個行為和 XmlResolver 屬性沒有設定並保留為預設狀態時的行為相同。This behavior is identical to the behavior when the XmlResolver property is not set, but left in its default state.

XmlDocument 使用匿名權限執行所有的動作。The XmlDocument uses anonymous permissions for all actions.
屬性會設定為 null (在 Microsoft Visual Basic .NET中則為 Nothing)。The property is set to null (Nothing in Microsoft Visual Basic .NET). 不支援需要外部資源的功能,例如尋找外部結構描述或 DTD。No features that require an external resource are supported, such as locating an external schema or DTD. 也不會解析外部實體,並且也不支援執行編輯功能,例如插入需要資源的實體節點。Nor will external entities be resolved, and performing editing functions such as inserting entity nodes that require resolution, are not supported. 若屬性是 null,則不論 XmlDocument 是完全受信任或非完全受信任,都會有相同的行為。When the property is null, the behavior is the same regardless if the XmlDocument is fully trusted or semi-trusted.
尚未設定屬性,但是保留為預設狀態。The property is not set, but left in its default state. 在解析檔案名稱、尋找外部 DTD、實體和結構描述時,帶有 null 認證的 XmlUrlResolver 會被具現化,並且被 XmlDocument 使用,同時在編輯節點時會使用 null 認證。An XmlUrlResolver with null credentials will be instantiated and used by the XmlDocument when resolving file names, locating external DTDs, entities, and schemas, and null credentials are used when editing nodes. XmlDocument 使用匿名權限執行所有的動作。The XmlDocument uses anonymous permissions for all actions.

Load 的輸入為 XmlReader 並且 XmlDocument 是非完全信任時,這個表格適用於 XmlDocument.Load 方法。This table applies to the XmlDocument.Load method when the input to the Load is an XmlReader, and the XmlDocument is semi-trusted.

XmlResolver 屬性XmlResolver property 功能Function 注意Notes
XmlDocument 使用的 XmlResolver 類別與 XmlReader 使用的類別相同。The XmlResolver class used by the XmlDocument is the same one being used by the XmlReader. XmlDocument 使用指定給 XmlReaderXmlResolverThe XmlDocument uses the XmlResolver that was assigned to the XmlReader.

因為 XmlDocument.Resolver 是從 XmlReader 取得 XmlResolver,所以不論 XmlDocument 的信任層級為何,其屬性都無法設定。The XmlDocument.Resolver property cannot be set, regardless of the XmlDocument trust level, because it is getting an XmlResolver from the XmlReader. 您無法藉由設定 XmlDocumentXmlResolver 屬性,來覆寫 XmlReaderXmlResolver 的設定。You cannot attempt to override the settings of the XmlReaders XmlResolver by setting the XmlResolver property of the XmlDocument.
XmlReader 可以是 XmlTextReader、驗證的 XmlReader 或自訂撰寫的讀取器。The XmlReader can be the XmlTextReader, validating XmlReader, or a custom-written reader. 如果所用的讀取器支援實體解析,則會解析外部實體。If the reader used supports entity resolution, external entities are resolved. 如果傳遞的讀取器不支援實體參考,則不會解析實體參考。If the reader passed in does not support entity references, then entity references are not resolved.

設定 XmlResolver 使其含有正確的認證,即可存取外部資源。Setting the XmlResolver to contain the correct credentials allows access to external resources.

注意

您無法擷取 XmlResolver 屬性。There is no way to retrieve the XmlResolver property. 這有助於防止使用者重複使用已經在其上設定安全性認證的 XmlResolverThis helps to prevent a user from reusing an XmlResolver on which credentials have been set. 此外,若使用 XmlTextReader 或驗證的 XmlReader 來載入 XmlDocument,且 XmlDocument 具有已經設定的解析程式,則 XmlDocumentLoad 階段之後將不會快取這些讀取器的解析程式,因為這也會帶來安全性方面的風險。Also, if an XmlTextReader or validating XmlReader is used to load the XmlDocument and the XmlDocument has a resolver that has been set, the resolvers from these readers are not cached by the XmlDocument after the Load phase, since this also presents a security risk.

如需詳細資訊,請參閱 XmlResolver 參考頁面的<備註>一節。For more information, see the Remarks section of the XmlResolver reference page.

請參閱See Also

XML 文件物件模型 (DOM)XML Document Object Model (DOM)