Export (0) Print
Expand All

XmlValidatingReader.ValidationType Property

Gets or sets a value indicating the type of validation to perform.

Namespace: System.Xml
Assembly: System.Xml (in system.xml.dll)

public ValidationType ValidationType { get; set; }
/** @property */
public ValidationType get_ValidationType ()

/** @property */
public void set_ValidationType (ValidationType value)

public function get ValidationType () : ValidationType

public function set ValidationType (value : ValidationType)

Property Value

One of the ValidationType values. If this property is not set, it defaults to ValidationType.Auto.

Exception typeCondition

InvalidOperationException

Setting the property after a Read has been called.

NoteNote

The XmlValidatingReader class is obsolete in Microsoft .NET Framework version 2.0. You can create a validating XmlReader instance by using the XmlReaderSettings class and the Create method. For more information, see Validating XML Data with XmlReader.

This property must be set before the first call to Read. Setting this property to ValidationType.None creates a non-validating reader.

If external document type definitions (DTDs) or schemas are needed for validation, the XmlResolver is used.

For more information, see Validation of XML with XmlValidatingReader and Validation Types of the XmlValidatingReader.

The following example validates two files.

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

public class Sample
{
  private const String doc1 = "notValid.xml";
  private const String doc2 = "cdDTD.xml";
  private const String doc3 = "book1.xml";

  private XmlTextReader txtreader = null;
  private XmlValidatingReader reader = null;
  private Boolean m_success = true;

  public Sample ()
  {
      //Parse the files and validate when requested.
      Validate(doc1, ValidationType.XDR);  //Validation should fail.
      Validate(doc2, ValidationType.DTD);  //Validation should fail.
      Validate(doc3, ValidationType.None); //No validation performed.

  }    

  public static void Main ()
  {
      Sample validation = new Sample();
  }

  private void Validate(String filename, ValidationType vt)
  {
    try
    {    
        //Implement the readers.  Set the ValidationType.
        txtreader = new XmlTextReader(filename);
        reader = new XmlValidatingReader(txtreader);
        reader.ValidationType = vt;

        //If the reader is set to validate, set the event handler.
        if (vt==ValidationType.None)
           Console.WriteLine("\nParsing XML file " + filename.ToString());
        else{
           Console.WriteLine("\nValidating XML file " + filename.ToString());
           m_success = true;
           //Set the validation event handler.
           reader.ValidationEventHandler += new ValidationEventHandler (ValidationCallBack);
        }

        // Read XML data
        while (reader.Read()){}

        if (vt==ValidationType.None)
           Console.WriteLine("Finished parsing file.");
        else
          Console.WriteLine ("Validation finished. Validation {0}", (m_success==true ? "successful" : "failed"));
     }

     finally
     {
        //Close the reader.
        if (reader != null)
          reader.Close();
     } 

  }
  
  //Display the validation errors.
  private void ValidationCallBack (object sender, ValidationEventArgs args)
  {
     m_success = false;

     Console.Write("\r\n\tValidation error: " + args.Message);

  }
}

import System.*;
import System.IO.*;
import System.Xml.*;
import System.Xml.Schema.*;

public class Sample
{
    private String doc1 = "notValid.xml";
    private String doc2 = "cdDTD.xml";
    private String doc3 = "book1.xml";
    private XmlTextReader txtReader = null;
    private XmlValidatingReader reader = null;
    private boolean mSuccess = true;

    public Sample()
    {
        //Parse the files and validate when requested.
        Validate(doc1, ValidationType.XDR); //Validation should fail.
        Validate(doc2, ValidationType.DTD); //Validation should fail.
        Validate(doc3, ValidationType.None); //No validation performed.
    } //Sample

    public static void main(String[] args)
    {
        Sample validation =  new Sample();
    } //main

    private void Validate(String fileName, ValidationType vt)
    {
        try {
            //Implement the readers.  Set the ValidationType.
            txtReader = new XmlTextReader(fileName);
            reader = new XmlValidatingReader(txtReader);
            reader.set_ValidationType(vt);

            //If the reader is set to validate, set the event handler.
            if (vt.Equals(ValidationType.None)) {
                Console.WriteLine("\nParsing XML file " 
				    + fileName.ToString());
            }
            else {
                Console.WriteLine("\nValidating XML file " 
				    + fileName.ToString());
                mSuccess = true;

                //Set the validation event handler.
                reader.add_ValidationEventHandler(new ValidationEventHandler
                    (ValidationCallBack));
            }
            // Read XML data
            while(reader.Read()) {

            } 
            if ( vt.Equals(ValidationType.None)) {
                Console.WriteLine("Finished parsing file.");
            }
            else {
                Console.WriteLine("Validation finished. Validation {0}",
                    (mSuccess == true) ? "successful" : "failed");
            }
        } 
        finally {
            //Close the reader.
            if (reader != null) {
                reader.Close();
            }
        }
    } //Validate

    //Display the validation errors.
    private void ValidationCallBack(Object sender, ValidationEventArgs args) 
    {
        mSuccess = false;
        Console.Write("\r\n\tValidation error: " + args.get_Message());
    } //ValidationCallBack 
} //Sample

The sample uses the following four input files:

notValid.xml (The "x-schema:" prefix identifies the XML-Data Reduced (XDR) schema for the reader.)


<?xml version='1.0'?>
<bookstore xmlns="x-schema:schema1.xdr">
  <book genre="novel" style="hardcover">
    <title>The Handmaid's Tale</title>
    <author>
      <first-name>Margaret</first-name>
      <last-name>Atwood</last-name>
    </author>
    <price>19.95</price>
  </book>
  <book genre="novel" style="other">
    <title>The Poisonwood Bible</title>
    <author>
      <first-name>Barbara</first-name>
      <last-name>Kingsolver</last-name>
    </author>
    <price>11.99</price>
  </book>
</bookstore>

schema1.xdr


<?xml version="1.0"?>
 <Schema xmlns="urn:schemas-microsoft-com:xml-data"
         xmlns:dt="urn:schemas-microsoft-com:datatypes">
   <ElementType name="first-name" content="textOnly"/>
   <ElementType name="last-name" content="textOnly"/>
   <ElementType name="name" content="textOnly"/>
   <ElementType name="price" content="textOnly" dt:type="fixed.14.4"/>
   <ElementType name="author" content="eltOnly" order="one">
     <group order="seq">
       <element type="name"/>
     </group>
     <group order="seq">
       <element type="first-name"/>
       <element type="last-name"/>
     </group>
   </ElementType>
   <ElementType name="title" content="textOnly"/>
   <AttributeType name="genre" dt:type="string"/>
   <AttributeType name="style" dt:type="enumeration"
         dt:values="paperback hardcover"/>
   <ElementType name="book" content="eltOnly">
     <attribute type="genre" required="yes"/>
     <attribute type="style" required="yes"/>
     <element type="title"/>
     <element type="author"/>
     <element type="price"/>
   </ElementType>
   <ElementType name="bookstore" content="eltOnly">
     <element type="book"/>
   </ElementType>
 </Schema>

cdDTD.xml


<!--XML file using a DTD-->
<!DOCTYPE bookstore [
  <!ELEMENT bookstore (cd)*> 
  <!ELEMENT cd (title,artist,price)>
  <!ATTLIST cd genre CDATA #REQUIRED>
  <!ELEMENT title (#PCDATA)>
  <!ELEMENT artist (#PCDATA)>
  <!ELEMENT price (#PCDATA)>]>
<bookstore>
  <cd genre="alternative"  ISBN="2-3631-4">
    <title>Americana</title>
    <artist>Offspring</artist>
    <price>16.95</price>
  </cd>
</bookstore>

book1.xml


<?xml version='1.0' ?>
<!DOCTYPE book [<!ENTITY h 'hardcover'>]>
<book>
  <title>Pride And Prejudice</title>
  <misc>&h;</misc>
</book>

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft