XmlSchemaIntegrityValidation Class
The XmlSchemaIntegrityValidation class provides a way to verify integrity constraints of an XML schema at runtime. It is part of the System.Xml namespace.
Syntax
public sealed class XmlSchemaIntegrityValidation
{
public XmlSchemaIntegrityValidation();
public XmlSchemaIntegrityValidation(string schemaFile);
public void Load(string schemaFile);
public bool Validate(XmlReader xmlReader);
public ValidationEventHandler ValidationEventHandler { get; set; }
public XmlResolver XmlResolver { get; set; }
}
Remarks
The class offers two ways to load a schema: via a constructor that accepts a file path, or by calling Load. The Validate method checks the supplied XML document against the loaded schema, raising validation events through the ValidationEventHandler delegate.
Typical usage patterns:
- Validate configuration files during application startup.
- Enforce data contracts in web services.
- Perform ad‑hoc validation in development tools.
Examples
The following example demonstrates how to validate an XML file against a schema.
using System;
using System.Xml;
using System.Xml.Schema;
class Program
{
static void Main()
{
var validator = new XmlSchemaIntegrityValidation("schema.xsd");
validator.ValidationEventHandler += (sender, e) =>
{
Console.WriteLine($"{e.Severity}: {e.Message}");
};
using (var reader = XmlReader.Create("data.xml"))
{
bool isValid = validator.Validate(reader);
Console.WriteLine(isValid ? "Document is valid." : "Document is invalid.");
}
}
}
See Also
| Related Types | Description |
|---|---|
| XmlReader | Provides a fast, non-cached, forward-only way to read XML data. |
| XmlWriter | Writes XML data in a forward-only manner. |
| XmlSchema | Represents an XML Schema definition language (XSD) schema. |