Validate an XML Document Against a Schema

Problem

You want to ensure that an XML document conforms to an XML schema.

Solution

Use XmlValidatingReader and handle the ValidationEventHandler event.

Discussion

An XML schema defines the rules that a given type of XML document must follow. The schema includes rules that define

  • The elements and attributes that can appear in a document.

  • The data types for elements and attributes.

  • The structure of a document, including what elements are children of other elements.

  • The order and number of child elements that appear in a document.

  • Whether elements are empty, can include text, or require fixed values.

XML schema documents are beyond the scope of this chapter, but much can be learned from a simple example. Essentially, ...

Get Microsoft® Visual Basic® .NET Programmer's Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.