System.Xml.Schema Namespace

Namespace Summary

The System.Xml.Schema namespace contains the XML Schema definition language (XSD) implementation, which provides classes that enable you to create, edit, validate and navigate through XML Schemas.

Key Classes

ClassDescription
XmlReaderProvides a fast, non-cached, forward-only way of reading XML data.
XmlReaderSettingsSpecifies a set of features to support on the XmlReader object created by the Create method.
XmlSchemaRepresents an XML Schema definition language (XSD) schema.
XmlValidatingReaderValidates XML data using XML Schemas or DTDs while reading.

Sample Code

using System;
using System.Xml;
using System.Xml.Schema;

class Program
{
    static void Main()
    {
        XmlReaderSettings settings = new XmlReaderSettings();
        settings.ValidationType = ValidationType.Schema;
        settings.Schemas.Add(null, "example.xsd");
        settings.ValidationEventHandler += (sender, e) =>
        {
            Console.WriteLine($"Validation {e.Severity}: {e.Message}");
        };

        using (XmlReader reader = XmlReader.Create("example.xml", settings))
        {
            while (reader.Read()) { }
        }
    }
}

Related Resources