Export (0) Print
Expand All

XmlReaderSettings.DtdProcessing Property

Gets or sets a value that determines the processing of DTDs.

Namespace:  System.Xml
Assemblies:   System.Xml (in System.Xml.dll)
  System.Xml.ReaderWriter (in System.Xml.ReaderWriter.dll)

public DtdProcessing DtdProcessing { get; set; }

Property Value

Type: System.Xml.DtdProcessing
One of the enumeration values that determines the processing of DTDs. The default is Prohibit.

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

This property can have one of the following values:

To perform validation against a DTD, the XmlReader 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. To validate an XML file against a DTD:

  • Set the XmlReaderSettings.DtdProcessing property to DtdProcessing.Parse.

  • Set the XmlReaderSettings.ValidationType property to ValidationType.DTD.

  • If the DTD is an external file stored on a network resource that requires authentication, pass an XmlResolver object with the necessary credentials to the Create method.

Important noteImportant

If the DtdProcessing property is set to DtdProcessing.Ignore, the XmlReader will not report the DTDs. This means that the DTD/DOCTYPE will be lost on output.

The following example validates an XML file using a DTD file.

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

public class Sample {

  public static void Main() {

    // Set the validation settings.
    XmlReaderSettings settings = new XmlReaderSettings();
    settings.DtdProcessing = DtdProcessing.Parse;
    settings.ValidationType = ValidationType.DTD;
    settings.ValidationEventHandler += new ValidationEventHandler (ValidationCallBack);

    // Create the XmlReader object.
    XmlReader reader = XmlReader.Create("itemDTD.xml", settings);


    // Parse the file.  
    while (reader.Read());

  }

  // Display any validation errors. 
  private static void ValidationCallBack(object sender, ValidationEventArgs e) {
    Console.WriteLine("Validation Error: {0}", e.Message);
  }
}

Input

The example uses the itemDTD.xml file as input.

<!--XML file using a DTD-->
<!DOCTYPE store [
  <!ELEMENT store (item)*> 
  <!ELEMENT item (name,dept,price)>
  <!ATTLIST item type CDATA #REQUIRED>
  <!ELEMENT name (#PCDATA)>
  <!ELEMENT price (#PCDATA)>]>
<store>
  <item type="supplies"  ISBN="2-3631-4">
    <name>paint</name>
    <price>16.95</price>
  </item>
</store>

.NET Framework

Supported in: 4.6, 4

.NET Framework Client Profile

Supported in: 4

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft