XmlWriterSettings.ConformanceLevel 属性

定义

获取或设置的 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:

根级别的文本、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.

如果存在冲突,例如在根级别尝试写入文本节点和 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调用后跟一个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/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.
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.

适用于