Share via


XmlValidatingReader.ReadTypedValue Metodo

Definizione

Ottiene il tipo Common Language Runtime per il tipo XSD (XML Schema Definition Language) specificato.

public:
 System::Object ^ ReadTypedValue();
public object? ReadTypedValue ();
public object ReadTypedValue ();
member this.ReadTypedValue : unit -> obj
Public Function ReadTypedValue () As Object

Restituisce

Object

Tipo Common Language Runtime per il tipo XML Schema specificato.

Esempio

Nell'esempio seguente vengono visualizzate le informazioni sul tipo per ogni elemento del documento XML.

#using <System.Xml.dll>
#using <System.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Schema;

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

public:
   static void main()
   {
      XmlTextReader^ tr = gcnew XmlTextReader( "booksSchema.xml" );
      XmlValidatingReader^ vr = gcnew XmlValidatingReader( tr );
      vr->Schemas->Add( nullptr, "books.xsd" );
      vr->ValidationType = ValidationType::Schema;
      vr->ValidationEventHandler += gcnew ValidationEventHandler( Sample::ValidationCallBack );
      while ( vr->Read() )
      {
         if ( vr->NodeType == XmlNodeType::Element )
         {
            if ( dynamic_cast<XmlSchemaComplexType^>(vr->SchemaType) != nullptr )
            {
               XmlSchemaComplexType^ sct = dynamic_cast<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 );
            }
         }
      }
   }
};

int main()
{
   Sample::main();
}
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);
  }
}
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

Nell'esempio vengono usati i file di input seguenti.

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>

Commenti

Nota

La XmlValidatingReader classe è obsoleta in .NET Framework 2.0. È possibile creare un'istanza di XmlReader convalida usando la XmlReaderSettings classe e il Create metodo . Per altre informazioni, vedere la sezione Note della pagina di riferimento XmlReader.

Ad esempio, se il tipo di schema è definito come xsd:int, viene restituito il tipo di int32 runtime per l'oggetto . Questa operazione può essere testata usando il metodo ed eseguire il GetType cast di conseguenza. Il ReadTypedValue metodo restituisce sempre un tipo derivato dalla Object classe . Non restituisce mai il Object tipo.

Se il lettore è posizionato su un attributo, questo metodo restituisce l'oggetto tipizzato di runtime, ma non modifica la posizione del lettore. Se il lettore è posizionato su un elemento, ReadTypedValue legge qualsiasi nodo CDATA, testo, spazio vuoto, spazio vuoto significativo e nodi di commento, restituisce l'oggetto tipizzato di runtime e posiziona il lettore sul tag finale. Tutti i tipi che non dispongono di un mapping diretto, ad esempio NMTOKENS, vengono restituiti come stringhe.

Nota

Se ValidationType è stato impostato su ValidationType.None, le informazioni sul tipo di dati non vengono fornite da schemi o definizioni dei tipi di documento (DTD).

Attenzione

Dopo aver chiamato Close, ReadTypedValue restituirà null.

Si applica a

Vedi anche