XmlReaderSettings.ConformanceLevel 属性

定义

获取或设置 XmlReader 将遵循的一致性级别。Gets or sets the level of conformance which the XmlReader will comply.

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

属性值

指定一致性级别(XML 读取器将强制该级别)的枚举值之一。One of the enumeration values that specifies the level of conformance that the XML reader will enforce. 默认值为 DocumentThe default is Document.

示例

下面的示例创建一个XmlReader读取 XML 片段的对象。The following example creates an XmlReader object that reads an XML fragment.

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 读取器满足以下符合性要求:XML readers that are created by the Create method meet the following compliance requirements by default:

  • 新行和属性值根据 W3C XML 1.0 建议进行标准化。New lines and attribute value are normalized according to the W3C XML 1.0 Recommendation.

  • 所有实体都将自动展开。All entities are automatically expanded.

  • 即使读取器不进行验证, 也始终会添加在文档类型定义中声明的默认属性。Default attributes declared in the document type definition are always added even when the reader does not validate.

  • 允许声明映射到正确的 XML 命名空间 URI 的 XML 前缀。Declaration of XML prefix mapped to the correct XML namespace URI is allowed.

  • 单个NotationType属性声明和NmTokens单个Enumeration属性声明中的表示法名称是不同的。The notation names in a single NotationType attribute declaration and NmTokens in a single Enumeration attribute declaration are distinct.

您可以使用ConformanceLevel属性来检查并确保正在读取的流符合格式正确的 XML 1.0 文档或文档片段的规则。You can use the ConformanceLevel property to check and guarantee that the stream being read complies with the rules for a well-formed XML 1.0 document or document fragment. 如果数据不符合, XmlException则会引发异常。If the data doesn't comply, an XmlException exception is thrown. 默认值为ConformanceLevel.Document (文档级一致性)。The default is ConformanceLevel.Document (document-level conformance).

这三个一致性级别是:The three conformance levels are:

设置Setting 说明Description
Document 确保 XML 数据符合格式正确的 XML 1.0 文档的规则。Ensures that the XML data conforms to the rules for a well-formed XML 1.0 document. 此级别的检查确保任何处理器都可以使用作为格式正确的 XML 1.0 文档读取的流。This level of checking ensures that any processor can consume the stream being read as an well-formed XML 1.0 document.

读取器检查下列事项:The reader checks for the following:

-顶级项不能包含除 XML 声明、文档类型定义 (DTD)、元素、注释、空格或处理指令以外的任何节点。- The top-level item must not have any nodes other than XML Declaration, document type definition (DTD), element, comment, white space, or processing instruction.
-XML 数据必须刚好有一个顶级元素节点。- The XML data must have exactly one top-level element node.
Fragment 确保 XML 数据符合格式正确的 XML 1.0 文档片段的规则。Ensures that the XML data conforms to the rules for a well-formed XML 1.0 document fragment.

此设置接受包含多个根元素的 XML 数据或位于顶级的文本节点。This setting accepts XML data with multiple root elements, or text nodes at the top-level. 此级别的检查确保任何处理器都可以使用作为外部分析的实体读取的流。This level of checking ensures that any processor can consume the stream being read as an external parsed entity.
Auto 指定读取器应根据传入数据确定一致性检查的级别。Specifies that the reader should determine the level of conformance checking based on the incoming data.

如果 XML 数据包含 DTD 信息,将应用文档一致性检查。Document conformance checking is applied if the XML data contains DTD information.

如果 XML 数据包含下列某一项,将应用片断一致性检查。Fragment conformance checking is applied if the XML data contains one of following:

根级别的文本、CDATA 或实体引用节点。- Text, CDATA, or entity reference node at the root level.
-根级别有多个元素。- More than one element at the root level.
-根级别上没有元素。- No element at the root level.

如果存在冲突,例如在根级别存在文本节点和 DTD 时,将引发 XmlExceptionAn XmlException is thrown if there is a conflict, such as when there is a text node and a DTD at the root level.

如果使用 Create 方法为现有 XmlReader 添加附加功能,可以在包装方案中使用此设置。This setting can be used in wrapping scenarios when the Create method is used to add additional features to an existing XmlReader. 在这种情况ConformanceLevel.Document下, 不会添加任何新的一致性检查。In this case, ConformanceLevel.Document does not add any new conformance checking. 一致性检查留给正在包装的 XmlReaderConformance checking is left to the XmlReader that is being wrapped.

备注

如果存在 DTD,XML 1.0 建议要求文档级别的一致性。The XML 1.0 recommendation requires document-level conformance when a DTD is present. 因此, 如果读取器配置为支持ConformanceLevel.Fragment, 但 XML 数据包含文档类型定义 (DTD) XmlException , 则会引发。Therefore, if the reader is configured to support ConformanceLevel.Fragment, but the XML data contains a document type definition (DTD), an XmlException is thrown.

根据ConformanceLevel属性的设置, 读取器如何处理特定的一致性冲突:Here's how the reader handles specific conformance violations depending on the setting of the ConformanceLevel property:

条件Condition Document Fragment Auto
文本或类型化值显示在顶级。Text or a typed value appears at the top level. XmlException引发。XmlException is thrown. 不视为违反此设置。Not considered a violation for this setting. 不视为违反此设置。Not considered a violation for this setting.
多个元素或没有任何元素出现在顶级。Multiple elements or no element appear at the top level. XmlException引发。XmlException is thrown. 不视为违反此设置。Not considered a violation for this setting. 不视为违反此设置。Not considered a violation for this setting.
顶级项为空白。Top-level item is white space. 不视为违反此设置。Not considered a violation for this setting. 不视为违反此设置。Not considered a violation for this setting. 不视为违反此设置。Not considered a violation for this setting.
顶级项是一个特性 (可识别为文本节点)。Top-level item is an attribute (recognized as a text node). XmlException引发。XmlException is thrown. 不视为违反此设置。Not considered a violation for this setting. 不视为违反此设置。Not considered a violation for this setting.
发现多个连续的文本节点。Multiple, contiguous text nodes are found. 不视为违反此设置。Not considered a violation for this setting. 不视为违反此设置。Not considered a violation for this setting. 不视为违反此设置。Not considered a violation for this setting.
相同的命名空间前缀在同一本地范围内声明了两次。The same namespace prefix is declared twice in the same local scope. XmlException引发。XmlException is thrown. XmlException引发。XmlException is thrown. XmlException引发。XmlException is thrown.
元素或属性中的命名空间在本地范围中不存在。The namespace in an element or attribute doesn't exist in the local scope. XmlException引发。XmlException is thrown. XmlException引发。XmlException is thrown. XmlException引发。XmlException is thrown.
数据包含前缀-命名空间不匹配。Data contains a prefix-namespace mismatch. XmlException引发。XmlException is thrown. XmlException引发。XmlException is thrown. XmlException引发。XmlException is thrown.
xml:space属性包含无效的值。xml:space attribute contains an invalid 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.
前缀与http://www.w3.org/XML/1998/namespace URI 不匹配。 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.

适用于

另请参阅