Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

XmlReaderSettings.ConformanceLevel Property

Gets or sets the level of conformance which the XmlReader will comply.

Namespace:  System.Xml
Assembly:  System.Xml (in System.Xml.dll)
public ConformanceLevel ConformanceLevel { get; set; }

Property Value

Type: System.Xml.ConformanceLevel
One of the enumeration values that specifies the level of conformance that the XML reader will enforce. The default is ConformanceLevel.Document.

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.

NoteNote

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.

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);

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.