XmlValidatingReader.SchemaType Proprietà

Definizione

Ottiene un oggetto di tipo schema.

public:
 property System::Object ^ SchemaType { System::Object ^ get(); };
public object? SchemaType { get; }
public object SchemaType { get; }
member this.SchemaType : obj
Public ReadOnly Property SchemaType As Object

Valore della proprietà

Object

XmlSchemaDatatype, XmlSchemaSimpleType o XmlSchemaComplexType a seconda che il valore del nodo sia un tipo XSD (XML Schema Definition Language) incorporato o un tipo simpleType o complexType definito dall'utente; null se il nodo corrente non ha nessun tipo di schema.

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.

L'utente deve testare il tipo restituito. Ad esempio,

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  
       }  

Se viene eseguita la convalida di XML Schema, l'oggetto XmlSchemaType o XmlSchemaDatatype corrisponde all'elemento corrente letto. Se viene eseguita la definizione del tipo di documento (convalida DTD), questa proprietà restituisce null.

XmlSchemaDatatype viene restituito se l'elemento corrente o l'attributo è un tipo semplice che può specificare vincoli di convalida speciali per i tipi semplici, ad esempio min e max.

XmlSchemaSimpleType viene restituito se l'elemento o l'attributo corrente è un simpleType definito dall'utente.

XmlSchemaComplexType viene restituito se l'elemento corrente è un complexType definito dall'utente. Questo tipo non può essere restituito dagli attributi.

Nota

Se ValidationType è stato impostato su ValidationType.None, non vengono fornite informazioni sul tipo di dati da schemi o DTD.

Attenzione

Dopo la chiamata Closea , SchemaType restituirà Null.

Si applica a

Vedi anche