XmlValidatingReader.SchemaType 属性

定义

获取架构类型对象。

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

属性值

Object

XmlSchemaDatatypeXmlSchemaSimpleTypeXmlSchemaComplexType,这取决于节点值是内置 XML 架构定义语言 (XSD) 类型还是用户定义的 simpleType 或 complexType;如果当前节点没有架构类型,则为 null

示例

以下示例显示 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

此示例使用以下输入文件。

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>

注解

备注

XmlValidatingReader在 .NET Framework 2.0 中已过时。 可以使用类和Create方法创建验证XmlReader实例XmlReaderSettings。 有关详细信息,请参阅 XmlReader 引用页的“备注”部分。

用户需要测试返回的类型。 例如,

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  
       }  

如果要执行 XML 架构验证,或XmlSchemaType``XmlSchemaDatatype对应于正在读取的当前元素。 如果执行文档类型定义 (DTD 验证) ,则此属性返回 null

XmlSchemaDatatype 如果当前元素或属性是一种简单类型,可以指定简单类型的特殊验证约束,例如 min 和 max。

XmlSchemaSimpleType 如果当前元素或属性是用户定义的 simpleType,则返回 。

XmlSchemaComplexType 如果当前元素是用户定义的 complexType,则返回 。 此属性无法返回此类型。

备注

如果 ValidationType 已设置为 ValidationType.None,则不会从架构或 DTD 中提供任何数据类型信息。

注意

调用 Close后,SchemaType 将返回 Null。

适用于

另请参阅