Validation of XML with Schemas

To define the structure of an XML document, as well as its element relationships, data types, and content constraints, you use a document type definition (DTD) or schema. While an XML document is considered to be well-formed if it meets all the syntactical requirements defined by the World Wide Web Consortium (W3C) Extensible Markup Language (XML) 1.0 Recommendation, it is not considered to be valid unless it is both well-formed and also conforms to the constraints defined by its DTD or schema. Therefore, although all valid XML documents are well-formed, not all well-formed XML documents are valid.

For more information about XML, see the World Wide Web Consortium (W3C) Extensible Markup Language (XML) 1.0 (Second Edition) Recommendation located at http://www.w3.org/TR/2000/REC-xml-20001006. For more information about XML Schema, see the W3C XML Schema Part 1: Structures Recommendation and the XML Schema Part 2: Datatypes Recommendation, located at http://www.w3.org/TR/.

Validation of XML documents and XML fragments is enforced using the XmlValidatingReader class, which provides DTD, XML-Data Reduced (XDR), and XML Schema definition language (XSD) schema validation services by implementing the validity constraints defined by the W3C Recommendations.

The XmlValidatingReader class implements the XmlReader class, performing validation in a forward-only manner over a stream of XML. XmlValidatingReader can take an XmlTextReader as input. The XmlValidatingReader layers support over the XmlTextReader, and also supports the ability to parse XML fragments with the XmlParserContext class. For more information about parsing XML fragments, see Reading XML Fragments with the XmlValidatingReader.

Any properties set on the supplied XmlTextReader also apply to the containing XmlValidatingReader.

The following code example shows the XmlValidatingReader layering support over the XmlTextReader.

Dim tr as XmlTextReader = new XmlTextReader("Sample1.xml")
Dim vr as XmlValidatingReader = new XmlValidatingReader(tr)
[C#]
XmlTextReader tr = new XmlTextReader("Sample1.xml");
XmlValidatingReader vr = new XmlValidatingReader(tr);

In This Section

  • XML Schema Reference (XSD)
    Documents the XML Schema definition language (XSD), as defined by the World Wide Web Consortium (W3C).