XmlWriterSettings.ConformanceLevel XmlWriterSettings.ConformanceLevel XmlWriterSettings.ConformanceLevel XmlWriterSettings.ConformanceLevel Property

定義

取得或設定 XML 寫入器檢查 XML 輸出是否符合的一致性層級。Gets or sets the level of conformance that the XML writer checks the XML output for.

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

屬性值

其中一個指定一致性層級 (文件、片段或自動偵測) 的列舉值。One of the enumeration values that specifies the level of conformance (document, fragment, or automatic detection). 預設為 DocumentThe default is Document.

範例

下列範例會將 XML 片段寫入記憶體資料流程。The following example writes an XML fragment to a memory stream.

XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
settings.ConformanceLevel = ConformanceLevel.Fragment;
settings.CloseOutput = false;

// Create the XmlWriter object and write some content.
MemoryStream strm = new MemoryStream();
XmlWriter writer = XmlWriter.Create(strm, settings);
writer.WriteElementString("orderID", "1-456-ab");
writer.WriteElementString("orderID", "2-36-00a");
writer.Flush();
writer.Close();

// Do additonal processing on the stream.
Dim settings As XmlWriterSettings = New XmlWriterSettings()
settings.OmitXmlDeclaration = true
settings.ConformanceLevel = ConformanceLevel.Fragment
settings.CloseOutput = false

' Create the XmlWriter object and write some content.
Dim strm as MemoryStream = new MemoryStream()
Dim writer As XmlWriter = XmlWriter.Create(strm, settings)
writer.WriteElementString("orderID", "1-456-ab")
writer.WriteElementString("orderID", "2-36-00a")
writer.Flush()
writer.Close()

' Do additonal processing on the stream.

備註

XmlWriterSettings.ConformanceLevel屬性會將 XML 寫入器設定為檢查並保證所寫入的資料流程符合一組特定的規則。The XmlWriterSettings.ConformanceLevel property configures the XML writer to check and guarantee that the stream being written complies with a certain set of rules. 可以檢查 XML 資料, 以查看它是否符合格式正確之 XML 1.0 檔或檔片段的規則。The XML data can be checked to see that it conforms to the rules for a well-formed XML 1.0 document or a document fragment. 下表描述這三個設定。The following table describes the three settings. 預設為檔層級的一致性。The default is document-level conformance.

設定Setting 說明Description
Document 確保 XML 輸出符合格式正確之 XML 1.0 檔的規則, 並且可由任何符合規範的處理器處理。Ensures that the XML output conforms to the rules for a well-formed XML 1.0 document and can be processed by any conforming processor.

檔一致性檢查包含大部分的片段層級檢查, 而且也請確定下列事項:The document conformance checks include most of the fragment-level checks and also ensure the following:

-最上層專案沒有 XML 宣告、DTD、元素、批註、空白字元或處理指示以外的任何節點。- The top-level item doesn't have any nodes other than XML Declaration, DTD, element, comment, white space, or processing instruction.
-XML 資料只有一個最上層元素節點。- The XML data has one and only one top-level element node.

XML 寫入器不會剖析寫入的 DTD 資訊。The XML writer doesn't parse DTD information that is written. 使用者須負責確保 DTD 的格式正確。The user is responsible for ensuring that the DTD is well-formed.
Fragment 確保 XML 輸出符合格式正確之 XML 1.0 檔片段的規則Ensures that the XML output conforms to the rules for a well-formed XML 1.0 document fragment.

此設定會接受在最上層具有多個根項目或文位元組點的 XML 資料。This setting accepts XML data that has multiple root elements or text nodes at the top level. 此檢查層級可確保任何處理器都可使用做為 XML 1.0 外部剖析實體讀取的資料流。This level of checking ensures that any processor can consume the stream being read as an XML 1.0 external parsed entity.

檔片段中不允許 DTD 資訊。DTD information isn't allowed in document fragments.
Auto 指定 XML 寫入器應根據傳入的資料來判斷一致性檢查的層級。Specifies that the XML writer should determine the level of conformance checking based on the incoming data. 當您不知道產生的 XML 將是格式正確的 XML 檔或片段時, 此設定會很有用。This setting can be useful when you don't know whether the generated XML will be a well-formed XML document or a fragment.

文件一致性檢查適用於下列情況:Document conformance checking is applied in the following cases:

-如果WriteStartDocument已呼叫方法, 則為。- If the WriteStartDocument method is called.
-如果寫入 DTD 資訊, 則為。- If DTD information is written.

如果 XML 資料包含下列其中一項, 則會套用片段一致性檢查:Fragment conformance checking is applied if the XML data contains one of these:

-根層級的 Text、CDATA 或 EntityReference 節點。- Text, CDATA, or EntityReference node at the root level.
-根層級中有一個以上的元素。- More than one element at the root level.
-根層級沒有元素。- No element at the root level.

如果出現衝突 (如當您嘗試在根層級寫入 Text 節點及 DTD 時),則會擲回 XmlExceptionAn XmlException is thrown if there is a conflict, such as when you try to write a text node and a DTD at the root level.

如果您要包裝另XmlWriter一個物件, 則外部物件不會執行任何其他的一致性檢查; 這會留給基礎物件。If you are wrapping another XmlWriter object, the outer object doesn't do any additional conformance checking; this is left to the underlying object.

如果 XML 寫入器偵測到任何會違反指定的一致性層級的資訊, 則會擲回例外狀況。If the XML writer detects any information that would violate the specified level of conformance, it throws an exception. 在某些情況下, XML 寫入器會自動校正一致性錯誤。In some cases, the XML writer automatically corrects the conformance error. 例如, 寫入器會關閉未封閉的屬性, 而不擲回例外狀況。For example, the writer closes an unclosed attribute without throwing an exception. 下表顯示如何在每個設定中處理各種一致性違規。The following table shows how various conformance violations are handled in each setting.

條件Condition Document Fragment Auto
找到 DTD 資訊。DTD information is found. 不違反此設定。Not a violation for this setting. 不過, XML 寫入器不會檢查 DTD;使用者必須確定 DTD 的格式正確。However, the XML writer doesn't check the DTD; the user must ensure that the DTD is well-formed. XmlException會擲回。XmlException is thrown. 套用檔一致性檢查。Document conformance checking is applied.
呼叫 WriteStartDocumentWriteStartDocument is called. 不視為違反此設定。Not considered a violation for this setting. XmlException會擲回。XmlException is thrown. 套用檔一致性檢查。Document conformance checking is applied.
多次呼叫 WriteStartDocumentWriteStartDocument is called multiple times. XmlException會擲回。XmlException is thrown. XmlException會擲回。XmlException is thrown. XmlException會擲回。XmlException is thrown.
文字值會出現在最上層 (不在元素或屬性節點底下)。A text value appears at the top level (not below an element or attribute node). XmlException會擲回。XmlException is thrown. 未被視為違規。Not considered a violation. 套用片段一致性檢查。Fragment conformance checking is applied.
最上層不會出現多個元素或專案。Multiple elements or no elements appear at the top level. XmlException會擲回。XmlException is thrown. 未被視為違規。Not considered a violation. 套用片段一致性檢查。Fragment conformance checking is applied.
最上層專案是空白字元。Top-level item is white space. 未被視為違規。Not considered a violation. 未被視為違規。Not considered a violation. 未被視為違規。Not considered a violation.
WriteEndAttribute不會呼叫來結束屬性節點。WriteEndAttribute isn't called to end an attribute node. 已由 XML 寫入器修正。Fixed by XML writer. 已由 XML 寫入器修正。Fixed by XML writer. 已由 XML 寫入器修正。Fixed by XML writer.
XML 寫入器處於不正確的嵌套狀態 (例如, WriteStartElement呼叫後面接著 a WriteEndAttribute)。The XML writer is in an improperly nested state (for example, a WriteStartElement call is followed by a WriteEndAttribute). XmlException會擲回。XmlException is thrown. XmlException會擲回。XmlException is thrown. XmlException會擲回。XmlException is thrown.
最上層專案是屬性。Top-level item is an attribute. XmlException會擲回。XmlException is thrown. XmlException會擲回。XmlException is thrown. XmlException會擲回。XmlException is thrown.
找到多個連續的文字節點。Multiple, contiguous text nodes are found. 不會被視為違規, 但使用者必須負責串連文位元組點。Not considered a violation, but it's the responsibility of the user to concatenate the text nodes. 不會被視為違規, 但使用者必須負責串連文位元組點。Not considered a violation, but it's the responsibility of the user to concatenate the text nodes. 不會被視為違規, 但使用者必須負責串連文位元組點。Not considered a violation, but it's the responsibility of the user to concatenate the text nodes.
同一命名空間前置詞在一個項目中宣告兩次。The same namespace prefix is declared twice in an element. XML 寫入器會為第二個命名空間產生新的前置詞。The XML writer generates a new prefix for the second namespace. XML 寫入器會為第二個命名空間產生新的前置詞。The XML writer generates a new prefix for the second namespace. XML 寫入器會為第二個命名空間產生新的前置詞。The XML writer generates a new prefix for the second namespace.
WriteStartElement指定未在該範圍內宣告的前置詞和命名空間, 或前置詞與不同的命名空間相關聯。WriteStartElement specifies a prefix and a namespace that isn't declared within that scope, or the prefix is associated with a different namespace. XML 寫入器會寫入必要的命名空間節點。The XML writer writes the necessary namespace node. XML 寫入器會寫入必要的命名空間節點。The XML writer writes the necessary namespace node. XML 寫入器會寫入必要的命名空間節點。The XML writer writes the necessary namespace node.
WriteStartAttribute指定在該範圍內未宣告的前置詞和命名空間。WriteStartAttribute specifies a prefix and a namespace that isn't declared within that scope. XML 寫入器會寫入必要的命名空間節點。The XML writer writes the necessary namespace node. XML 寫入器會寫入必要的命名空間節點。The XML writer writes the necessary namespace node. XML 寫入器會寫入必要的命名空間節點。The XML writer writes the necessary namespace node.
WriteStartAttribute指定前置詞和命名空間, 但前置詞與不同範圍內的命名空間相關聯。WriteStartAttribute specifies a prefix and a namespace, but the prefix is associated with a different in-scope namespace. XML 寫入器會忽略指定的前置詞, 並查閱正確的前置詞, 或產生新的前置詞。The XML writer ignores the specified prefix and either looks up the correct prefix or generates a new prefix. XML 寫入器會忽略指定的前置詞, 並查閱正確的前置詞, 或產生新的前置詞。The XML writer ignores the specified prefix and either looks up the correct prefix or generates a new prefix. XML 寫入器會忽略指定的前置詞, 並查閱正確的前置詞, 或產生新的前置詞。The XML writer ignores the specified prefix and either looks up the correct prefix or generates a new prefix.
WriteQualifiedName是用來寫入專案內容, 方法是使用不存在於範圍內的命名空間。WriteQualifiedName is used to write element content by using a namespace that doesn't exist within the scope. XmlException會擲回。XmlException is thrown. XmlException會擲回。XmlException is thrown. XmlException會擲回。XmlException is thrown.
WriteQualifiedName是用來寫入屬性內容, 方法是使用不存在於範圍內的命名空間。WriteQualifiedName is used to write attribute content by using a namespace that doesn't exist within the scope. XML 寫入器會寫入必要的命名空間宣告。The XML writer writes the necessary namespace declaration. XML 寫入器會寫入必要的命名空間宣告。The XML writer writes the necessary namespace declaration. XML 寫入器會寫入必要的命名空間宣告。The XML writer writes the necessary namespace declaration.
xml:space屬性未包含有效的值。The xml:space attribute doesn't contain a valid value. XmlException會擲回。XmlException is thrown. XmlException會擲回。XmlException is thrown. XmlException會擲回。XmlException is thrown.
發現無效的名稱。An invalid name is encountered. XmlException會擲回。XmlException is thrown. XmlException會擲回。XmlException is thrown. XmlException會擲回。XmlException is thrown.
xml前置詞不符合 URI。The xml prefix isn't matched to a URI. XML 寫入器不會檢查是否有這種類型的違規。The XML writer doesn't check for this type of violation. XML 寫入器不會檢查是否有這種類型的違規。The XML writer doesn't check for this type of violation. XML 寫入器不會檢查是否有這種類型的違規。The XML writer doesn't check for this type of violation.
首碼不符合http://www.w3.org/XML/1998/namespaceURI。 xmlThe xml prefix isn't matched to the http://www.w3.org/XML/1998/namespace URI. XmlException會擲回。XmlException is thrown. XmlException會擲回。XmlException is thrown. XmlException會擲回。XmlException is thrown.
xmlns首碼或本機名稱不符合 URI。The xmlns prefix or local name isn't matched to a URI. XML 寫入器不會檢查是否有這種類型的違規。The XML writer doesn't check for this type of violation. XML 寫入器不會檢查是否有這種類型的違規。The XML writer doesn't check for this type of violation. XML 寫入器不會檢查是否有這種類型的違規。The XML writer doesn't check for this type of violation.
首碼或本機名稱不符合http://www.w3.org/2000/xmlns URI。 xmlnsThe xmlns prefix or local name isn't matched to the http://www.w3.org/2000/xmlns URI. XmlException會擲回。XmlException is thrown. XmlException會擲回。XmlException is thrown. XmlException會擲回。XmlException is thrown.

適用於