System.Xml.XmlReaderSettings

Overview

The XmlReaderSettings class specifies a set of features to support on the XmlReader object created by the Create methods.

Use this class to customize validation, whitespace handling, conformance level, and other parsing options.

Namespace

System.Xml

Inheritance

Object → XmlReaderSettings

Constructors

Properties

PropertyTypeDescription
Async bool Gets or sets a value indicating whether to support async methods.
CloseInput bool Indicates whether the underlying stream or TextReader should be closed when the XmlReader is closed.
ConformanceLevel ConformanceLevel Specifies the type of XML content that can be processed: Document, Fragment or Auto.
DtdProcessing DtdProcessing Controls DTD processing behavior.
IgnoreComments bool When true, comments are ignored.
IgnoreProcessingInstructions bool When true, processing instructions are ignored.
IgnoreWhitespace bool When true, insignificant whitespace is ignored.
MaxCharactersFromEntities long Maximum number of characters that result from expanding entities.
MaxCharactersInDocument long Maximum number of characters in the document.
ProhibitDtd (obsolete) bool Obsolete. Use DtdProcessing instead.
Schemas XmlSchemaSet XML schemas used for validation.
ValidationFlags XmlSchemaValidationFlags Specifies the type of validation to perform.
ValidationType ValidationType Specifies the type of validation: None, DTD, Schema, or Auto.
XmlResolver XmlResolver Controls external resource resolution.

Methods

Examples

C#
VB.NET
using System;
using System.Xml;
using System.Xml.Schema;

class Program
{
    static void Main()
    {
        var settings = new XmlReaderSettings
        {
            DtdProcessing = DtdProcessing.Parse,
            ValidationType = ValidationType.Schema,
            ValidationFlags = XmlSchemaValidationFlags.ProcessIdentityConstraints |
                              XmlSchemaValidationFlags.ReportValidationWarnings,
            XmlResolver = new XmlUrlResolver()
        };

        settings.Schemas.Add(null, "schema.xsd");
        settings.ValidationEventHandler += (s, e) =>
        {
            Console.WriteLine($"{e.Severity}: {e.Message}");
        };

        using (XmlReader reader = XmlReader.Create("sample.xml", settings))
        {
            while (reader.Read()) { /* process document */ }
        }
    }
}
Imports System
Imports System.Xml
Imports System.Xml.Schema

Module Module1
    Sub Main()
        Dim settings As New XmlReaderSettings With
        {
            .DtdProcessing = DtdProcessing.Parse,
            .ValidationType = ValidationType.Schema,
            .ValidationFlags = XmlSchemaValidationFlags.ProcessIdentityConstraints Or _
                               XmlSchemaValidationFlags.ReportValidationWarnings,
            .XmlResolver = New XmlUrlResolver()
        }

        settings.Schemas.Add(Nothing, "schema.xsd")
        AddHandler settings.ValidationEventHandler, _
            Sub(s, e)
                Console.WriteLine($"{e.Severity}: {e.Message}")
            End Sub

        Using reader As XmlReader = XmlReader.Create("sample.xml", settings)
            While reader.Read()
                ' process document
            End While
        End Using
    End Sub
End Module

Related Topics