संपादित करें

इसके माध्यम से साझा किया गया


XmlReaderSettings.ConformanceLevel Property

Definition

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

Property Value

One of the enumeration values that specifies the level of conformance that the XML reader will enforce. The default is Document.

Examples

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)

Remarks

XML readers that are created by the Create method meet the following compliance requirements by default:

  • 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.

  • Declaration of XML prefix mapped to the correct XML namespace URI is allowed.

  • The notation names in a single NotationType attribute declaration and NmTokens in a single Enumeration attribute declaration are distinct.

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. If the data doesn't comply, an XmlException exception is thrown. The default is ConformanceLevel.Document (document-level conformance).

The three conformance levels are:

Setting Description
Document Ensures that the XML data conforms to the rules for a well-formed XML 1.0 document. 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:

- The top-level item must not have any nodes other than XML Declaration, document type definition (DTD), element, comment, white space, or processing instruction.
- The XML data must have exactly one top-level element node.
Fragment Ensures that the XML data conforms to the rules for a well-formed XML 1.0 document fragment.

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.

Document conformance checking is applied if the XML data contains DTD information.

Fragment conformance checking is applied if the XML data contains one of following:

- Text, CDATA, or entity reference node at the root level.
- More than one element at the root level.
- No element at the root level.

An XmlException is thrown if there is a conflict, such as when there is a text node and a DTD at the root level.

This setting can be used in wrapping scenarios when the Create method is used to add additional features to an existing XmlReader. In this case, ConformanceLevel.Document does not add any new conformance checking. Conformance checking is left to the XmlReader that is being wrapped.

Note

The XML 1.0 recommendation requires document-level conformance when a DTD is present. Therefore, if the reader is configured to support ConformanceLevel.Fragment, but the XML data contains a document type definition (DTD), an XmlException is thrown.

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 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 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 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 is thrown. XmlException is thrown. XmlException is thrown.
The namespace in an element or attribute doesn't exist in the local scope. XmlException is thrown. XmlException is thrown. XmlException is thrown.
Data contains a prefix-namespace mismatch. XmlException is thrown. XmlException is thrown. XmlException is thrown.
xml:space attribute contains an invalid value. XmlException is thrown. XmlException is thrown. XmlException is thrown.
An invalid name is encountered. XmlException is thrown. XmlException is thrown. XmlException is thrown.
The xml prefix isn't matched to the http://www.w3.org/XML/1998/namespace URI. XmlException is thrown. XmlException is thrown. XmlException is thrown.

Applies to

See also