Share via


Validation against DTD with the XmlValidatingReader

Document type definition (DTD) validation is implemented using the validity constraints defined in the World Wide Web Consortium (W3C) Extensible Markup Language (XML) 1.0 Recommendation. DTDs use a formal grammar to describe the structure and syntax of compliant XML documents; they specify content and values allowed for the XML document.

To perform validation against a document type definition (DTD), the XmlValidatingReader uses the DTD defined in the DOCTYPE declaration of an XML document. The DOCTYPE declaration can either point to an inline DTD or can be a reference to an external DTD file.

The following code example creates an XmlValidatingReader that takes an XmlTextReader. The input file, HeadCount.xml, is validated against an external DTD file, HeadCount.dtd. Any severity types and error messages are displayed.

Imports System
Imports System.IO
Imports System.Xml
Imports System.Xml.Schema

public class ValidationSample

      public shared sub Main()
         Dim tr As XmlTextReader = new XmlTextReader("HeadCount.xml")
         Dim vr As XmlValidatingReader = new XmlValidatingReader(tr)

         vr.ValidationType = ValidationType.DTD
         AddHandler vr.ValidationEventHandler, AddressOf ValidationCallback
         while(vr.Read())
         end while
         Console.WriteLine("Validation finished")

      end sub      

      public shared sub ValidationCallBack (sender As object, args As ValidationEventArgs)
      
         Console.WriteLine("***Validation error")
         Console.WriteLine("Severity:{0}", args.Severity)
         Console.WriteLine("Message:{0}", args.Message)
     end sub
end class
[C#]
using System;
using System.IO;
using System.Xml;
using System.Xml.Schema;

namespace ValidationSample
{
   class Sample
   {
      public static void Main()
      {
         XmlTextReader tr = new XmlTextReader("HeadCount.xml");
         XmlValidatingReader vr = new XmlValidatingReader(tr);

         vr.ValidationType = ValidationType.DTD;
         vr.ValidationEventHandler += new ValidationEventHandler (ValidationHandler);

         while(vr.Read());
         Console.WriteLine("Validation finished");
      }

      public static void ValidationHandler(object sender, ValidationEventArgs args)
      {
         Console.WriteLine("***Validation error");
         Console.WriteLine("\tSeverity:{0}", args.Severity);
         Console.WriteLine("\tMessage  :{0}", args.Message);
      }
   }
}

The following outlines the contents of the input file, HeadCount.xml, to be validated.

<!DOCTYPE HeadCount SYSTEM "HeadCount.dtd">
<HeadCount>
  <Name First="Waldo" Last="Pepper">
    <Name First="Salt" Last="Pepper" Relation="spouse"/>
    <Name First="Red" Last="Pepper" Relation="child"/>
  </Name>
  <Name First="&MyFirst;" Last="&MyLast;">
    <Name First="Sharon" Last="&MyLast;" Relation="spouse"/>
    <Name First="Morgan" Last="&MyLast;" Relation="child"/>
    <Name First="Shelby" Last="&MyLast;" Relation="child"/>
  </Name>
</HeadCount>

The following outlines the contents of the external DTD file, HeadCount.dtd, to be validated against.

<!ELEMENT HeadCount (Name)*>
<!ELEMENT Name (Name)*>
<!ATTLIST Name First CDATA #REQUIRED>
<!ATTLIST Name Last CDATA #REQUIRED>
<!ATTLIST Name Relation (self | spouse | child) "self">
<!ENTITY MyFirst "Jeff">
<!ENTITY MyLast "Smith">

See Also

Validation of XML with XmlValidatingReader | Validation of XML with Schemas | XmlValidatingReader.ValidationType Property