XmlValidatingReader.ReadTypedValue Method

Gets the common language runtime type for the specified XML Schema definition language (XSD) type.

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

public Object ReadTypedValue ()
public Object ReadTypedValue ()
public function ReadTypedValue () : Object
Not applicable.

Return Value

The common language runtime type for the specified XML Schema type.


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.

For example, if the schema type is defined as xsd:int, the runtime type int32 is returned for the object. This can be tested using the GetType method and cast accordingly. The ReadTypedValue method always returns a type that is derived from the Object class. It never returns the Object type.

If the reader is positioned on an attribute, this method returns the runtime typed object, but does not change the position of the reader. If the reader is positioned on an element, ReadTypedValue reads any CDATA, text, white space, significant white space, and comment nodes, returns the runtime typed object and positions the reader on the end tag. Any types that do not have a direct mapping, for example NMTOKENS, are returned as strings.


If ValidationType has been set to ValidationType.None, data type information is not provided from either schemas or document type definitions (DTDs).

Caution noteCaution:

After calling Close, ReadTypedValue will return a null reference (Nothing in Visual Basic).

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

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

public class Sample
    public static void main(String[] args)
        XmlTextReader tr = new XmlTextReader("booksSchema.xml");
        XmlValidatingReader vr = new XmlValidatingReader(tr);

        vr.get_Schemas().Add(null, "books.xsd");
        vr.add_ValidationEventHandler(new ValidationEventHandler(

        while (vr.Read()) {
            if (vr.get_NodeType().Equals(XmlNodeType.Element)) {
                if (vr.get_SchemaType() instanceof XmlSchemaComplexType) {
                    XmlSchemaComplexType sct = (XmlSchemaComplexType)(
                    Console.WriteLine("{0}({1})", vr.get_Name(), sct.get_Name());
                else {
                    Object value = vr.ReadTypedValue();
                    Console.WriteLine("{0}({1}):{2}", vr.get_Name(), 
                        value.GetType().get_Name(), value);
    } //main

    private static void ValidationCallBack(Object sender,
        ValidationEventArgs args)
        Console.WriteLine("***Validation error");
        Console.WriteLine("\tSeverity:{0}", args.get_Severity());
        Console.WriteLine("\tMessage  :{0}", args.get_Message());
    } //ValidationCallBack
} //Sample

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"/>


Windows 98, Windows Server 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 Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions