XmlValidatingReader.SchemaType Property


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Gets a schema type object.

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

public object SchemaType { get; }

Property Value

Type: System.Object

XmlSchemaDatatype, XmlSchemaSimpleType, or XmlSchemaComplexType depending whether the node value is a built in XML Schema definition language (XSD) type or a user defined simpleType or complexType; null if the current node has no schema type.


The XmlValidatingReader class is obsolete in .NET Framework 2.0. You can create a validating XmlReader instance by using the XmlReaderSettings class and the Create method. For more information, see the Remarks section of the XmlReader reference page.

The user needs to test for the returned type. For example,

object obj = vreader.SchemaType;
 if (obj is XmlSchemaType)
   XmlSchemaType st = (XmlSchemaType)obj;
   // use XmlSchemaType object
 if (obj is XmlSchemaDatatype)
   XmlSchemaDatatype sd = (XmlSchemaDatatype)obj;
   Type vt = sd.ValueType;
   // use XmlSchemaDatatype object

If XML Schema validation is being performed, the XmlSchemaType or XmlSchemaDatatype corresponds to the current element being read. If document type definition (DTD validation) is being performed, this property returns null.

XmlSchemaDatatype is returned if the current element, or attribute, is a simple type that can specify special validation constraints on the simple types, like min and max.

XmlSchemaSimpleType is returned if the current element, or attribute, is a user defined simpleType.

XmlSchemaComplexType is returned if the current element is a user defined complexType. This type cannot be returned by attributes.


If ValidationType has been set to ValidationType.None, no data type information is provided from either schemas or DTDs.


After calling Close, SchemaType will return Null.

The following example displays the type information for each of the elements in the XML document.

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

public class Sample{

  public static void Main(){

  XmlTextReader tr = new XmlTextReader("booksSchema.xml");
  XmlValidatingReader vr = new XmlValidatingReader(tr);

  vr.Schemas.Add(null, "books.xsd");
  vr.ValidationType = ValidationType.Schema;
  vr.ValidationEventHandler += new ValidationEventHandler (ValidationCallBack);

    if(vr.NodeType == XmlNodeType.Element){
      if(vr.SchemaType is XmlSchemaComplexType){
        XmlSchemaComplexType sct = (XmlSchemaComplexType)vr.SchemaType;
        Console.WriteLine("{0}({1})", vr.Name, sct.Name);
        object value = vr.ReadTypedValue();
        Console.WriteLine("{0}({1}):{2}", vr.Name, value.GetType().Name, value);

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

The example uses the following input files.


<?xml version='1.0'?>
<bookstore xmlns="urn:bookstore-schema">
  <book genre="autobiography">
    <title>The Autobiography of Benjamin Franklin</title>
  <book genre="novel">
    <title>The Confidence Man</title>


<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"

 <xsd:element name="bookstore" type="bookstoreType"/>

 <xsd:complexType name="bookstoreType">
  <xsd:sequence maxOccurs="unbounded">
   <xsd:element name="book"  type="bookType"/>

 <xsd:complexType name="bookType">
   <xsd:element name="title" type="xsd:string"/>
   <xsd:element name="author" type="authorName"/>
   <xsd:element name="price"  type="xsd:decimal"/>
  <xsd:attribute name="genre" type="xsd:string"/>

 <xsd:complexType name="authorName">
   <xsd:element name="first-name"  type="xsd:string"/>
   <xsd:element name="last-name" type="xsd:string"/>


.NET Framework
Available since 1.1
Return to top