XmlReaderSettings.ConformanceLevel 屬性

定義

取得或設定 XmlReader 要遵循的一致性層級。

public:
 property System::Xml::ConformanceLevel ConformanceLevel { System::Xml::ConformanceLevel get(); void set(System::Xml::ConformanceLevel value); };
public System.Xml.ConformanceLevel ConformanceLevel { get; set; }
member this.ConformanceLevel : System.Xml.ConformanceLevel with get, set
Public Property ConformanceLevel As ConformanceLevel

屬性值

ConformanceLevel

其中一個列舉值,指定 XML 讀取器將強制執行的一致性層級。 預設為 Document

範例

下列範例會 XmlReader 建立讀取 XML 片段的物件。

string xmlFrag ="<item rk:ID='abc-23'>hammer</item> " +
                        "<item rk:ID='r2-435'>paint</item>" +
                        "<item rk:ID='abc-39'>saw</item>";

// Create the XmlNamespaceManager.
NameTable nt = new NameTable();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);
nsmgr.AddNamespace("rk", "urn:store-items");

// Create the XmlParserContext.
XmlParserContext context = new XmlParserContext(null, nsmgr, null, XmlSpace.None);

// Create the reader.
XmlReaderSettings settings = new XmlReaderSettings();
settings.ConformanceLevel = ConformanceLevel.Fragment;
XmlReader reader = XmlReader.Create(new StringReader(xmlFrag), settings, context);
Dim xmlFrag As String = "<item rk:ID='abc-23'>hammer</item> " & _
                                     "<item rk:ID='r2-435'>paint</item>" & _
                                     "<item rk:ID='abc-39'>saw</item>"

' Create the XmlNamespaceManager.
Dim nt As New NameTable()
Dim nsmgr As New XmlNamespaceManager(nt)
nsmgr.AddNamespace("rk", "urn:store-items")

' Create the XmlParserContext.
Dim context As New XmlParserContext(Nothing, nsmgr, Nothing, XmlSpace.None)

' Create the reader. 
Dim settings As New XmlReaderSettings()
settings.ConformanceLevel = ConformanceLevel.Fragment
Dim reader As XmlReader = XmlReader.Create(New StringReader(xmlFrag), settings, context)

備註

方法所 Create 建立的 XML 讀取器預設符合下列合規性需求:

  • 新的行和屬性值會根據 W3C XML 1.0 建議正規化。

  • 所有實體都會自動展開。

  • 即使讀取器未驗證,也一律會新增在檔案類型定義中宣告的預設屬性。

  • 允許對應至正確 XML 命名空間 URI 的 XML 前置詞宣告。

  • NotationType 一屬性宣告和 NmTokensEnumeration 一屬性宣告中的標記法名稱是相異的。

您可以使用 ConformanceLevel 屬性來檢查並保證所讀取的資料流程符合格式正確的 XML 1.0 檔或檔片段的規則。 如果資料不符合規範, XmlException 則會擲回例外狀況。 預設值為 ConformanceLevel.Document 檔層級一致性 () 。

三個一致性層級如下:

設定 描述
Document 確保 XML 資料符合格式正確的 XML 1.0 檔規則。 此檢查層級可確保任何處理器都可以取用正在讀取的資料流程,做為 格式正確的 XML 1.0 檔

讀取器會檢查下列項目:

- 最上層專案不得具有 XML 宣告、檔案類型定義 (DTD) 、元素、批註、空白字元或處理指示以外的任何節點。
- XML 資料必須剛好有一個最上層元素節點。
Fragment 確保 XML 資料符合格式正確的 XML 1.0 檔片段規則。

此設定接受在最上層具有多個根項目或文字節點的 XML 資料。 此檢查層級可確保任何處理器都可以取用讀取為 外部剖析實體的資料流程。
Auto 指定讀取器應該根據傳入資料來判斷一致性檢查的層級。

如果 XML 資料包含 DTD 資訊,則會套用文件一致性檢查。

如果 XML 資料包含下列其中一項,則會套用片段一致性檢查:

- 根層級的文字、CDATA 或實體參考節點。
- 根層級的多個元素。
- 根層級沒有元素。

如果存在衝突 (如根層級存在文字節點及 DTD),則會擲回 XmlException

當使用 Create 方法向現有 XmlReader 加入其他功能時,此設定可以用於包裝案例。 在此情況下, ConformanceLevel.Document 不會新增任何新的一致性檢查。 一致性檢查會留給被包裝的 XmlReader

注意

存在 DTD 時,XML 1.0 版建議事項會要求文件層級的一致性。 因此,如果讀取器設定為支援 ConformanceLevel.Fragment ,但 XML 資料包含檔案類型定義 (DTD) , XmlException 則會擲回 。

以下是讀取器處理特定一致性違規的方式,視 屬性的 ConformanceLevel 設定而定:

條件 Document Fragment Auto
文字或具類型的值會出現在最上層。 XmlException 擲回 。 此設定未被視為違規。 此設定未被視為違規。
多個元素或最上層未顯示任何元素。 XmlException 擲回 。 此設定未被視為違規。 此設定未被視為違規。
最上層專案是空白字元。 此設定未被視為違規。 此設定未被視為違規。 此設定未被視為違規。
最上層專案是 (辨識為文位元組點) 的屬性。 XmlException 擲回 。 此設定未被視為違規。 此設定未被視為違規。
找到多個連續的文字節點。 此設定未被視為違規。 此設定未被視為違規。 此設定未被視為違規。
相同的命名空間前置詞會在相同的本機範圍中宣告兩次。 XmlException 擲回 。 XmlException 擲回 。 XmlException 擲回 。
專案或屬性中的命名空間不存在於本機範圍中。 XmlException 擲回 。 XmlException 擲回 。 XmlException 擲回 。
資料包含前置詞命名空間不符。 XmlException 擲回 。 XmlException 擲回 。 XmlException 擲回 。
xml:space 屬性包含不正確值。 XmlException 擲回 。 XmlException 擲回 。 XmlException 擲回 。
發現無效的名稱。 XmlException 擲回 。 XmlException 擲回 。 XmlException 擲回 。
前置 xml 詞與 URI 不相符 http://www.w3.org/XML/1998/namespace XmlException 擲回 。 XmlException 擲回 。 XmlException 擲回 。

適用於

另請參閱