.NET Framework Class Library
XmlValidatingReader.SchemaType Property

Gets a schema type object.

[Visual Basic]
Public ReadOnly Property SchemaType As Object
[C#]
public object SchemaType {get;}
[C++]
public: __property Object* get_SchemaType();
[JScript]
public function get SchemaType() : Object;

Property Value

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; a null reference (Nothing in Visual Basic) if the current node has no schema type.

CAUTION   After calling Close, SchemaType will return Null.

Remarks

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 (XSD) validation is being performed then the XmlSchemaType or XmlSchemaDatatype corresponds to the current element being read. If DTD validation is being performed this property returns a null reference (Nothing in Visual Basic).

XmlSchemaDatatype is returned if the current element, or attribute, is a simple type which 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.

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

Example

[Visual Basic, C#, C++] The following example displays the type information for each of the elements in the XML document.

[Visual Basic] 
Imports System
Imports System.IO
Imports System.Xml
Imports System.Xml.Schema

public class Sample

  public shared sub Main()
  
  Dim tr as XmlTextReader = new XmlTextReader("booksSchema.xml")
  Dim vr as XmlValidatingReader = new XmlValidatingReader(tr)
 
  vr.Schemas.Add(nothing, "books.xsd")
  vr.ValidationType = ValidationType.Schema
  AddHandler vr.ValidationEventHandler, AddressOf ValidationCallBack

  while(vr.Read())

    if(vr.NodeType = XmlNodeType.Element)
    
      if (vr.SchemaType.ToString() = "System.Xml.Schema.XmlSchemaComplexType")
        Dim sct as XmlSchemaComplexType = CType(vr.SchemaType,XmlSchemaComplexType)
        Console.WriteLine("{0}({1})", vr.Name, sct.Name)
      else      
        Dim value as object = vr.ReadTypedValue()
        Console.WriteLine("{0}({1}):{2}", vr.Name, value.GetType().Name, value)    
      end if
    end if
  end while
  end sub

  private 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;

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);
 
  while(vr.Read()){
    if(vr.NodeType == XmlNodeType.Element){
      if(vr.SchemaType is XmlSchemaComplexType){
        XmlSchemaComplexType sct = (XmlSchemaComplexType)vr.SchemaType;
        Console.WriteLine("{0}({1})", vr.Name, sct.Name);
      }
      else{
        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);
  }
}

[C++] 
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Schema;

__gc public class Sample
{
public:
   static void ValidationCallBack (Object* sender, ValidationEventArgs * args)
   {
      Console::WriteLine(S"***Validation error");
      Console::WriteLine(S"\tSeverity: {0}", __box(args -> Severity));
      Console::WriteLine(S"\tMessage  : {0}", args -> Message);
   }
};

int main()
{
   XmlTextReader* tr = new XmlTextReader(S"booksSchema.xml");
   XmlValidatingReader* vr = new XmlValidatingReader(tr);

   vr -> Schemas->Add(0, S"books.xsd");
   vr -> ValidationType = ValidationType::Schema;
   vr -> ValidationEventHandler += new ValidationEventHandler (vr, Sample::ValidationCallBack);

   while (vr -> Read())
   {
      if (vr -> NodeType == XmlNodeType::Element) 
      {
         if (dynamic_cast<XmlSchemaComplexType*>(vr -> SchemaType) != 0)
         {
            XmlSchemaComplexType* sct = dynamic_cast<XmlSchemaComplexType*>( vr -> SchemaType );
            Console::WriteLine(S" {0}( {1})", vr -> Name, sct -> Name);
         } 
         else
         {
            Object* value = vr -> ReadTypedValue();
            Console::WriteLine(S" {0}( {1}): {2}", vr -> Name, value->GetType()->Name, value);
         }
      }
   }
}

[Visual Basic, C#, C++] The example uses the following input files.

[Visual Basic, C#, C++] booksSchema.xml

<?xml version='1.0'?>
<bookstore xmlns="urn:bookstore-schema">
  <book genre="autobiography">
    <title>The Autobiography of Benjamin Franklin</title>
    <author>
      <first-name>Benjamin</first-name>
      <last-name>Franklin</last-name>
    </author>
    <price>8.99</price>
  </book>
  <book genre="novel">
    <title>The Confidence Man</title>
    <author>
      <first-name>Herman</first-name>
      <last-name>Melville</last-name>
    </author>
    <price>11.99</price>
  </book>
</bookstore>

books.xsd

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns="urn:bookstore-schema"
    elementFormDefault="qualified"
    targetNamespace="urn:bookstore-schema">

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

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

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

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

</xsd:schema>

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

XmlValidatingReader Class | XmlValidatingReader Members | System.Xml Namespace

Page view tracker