次の方法で共有


XmlValidatingReader.SchemaType プロパティ

スキーマ型のオブジェクトを取得します。

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

プロパティ値

ノード値が XML スキーマ定義言語 (XSD) 型またはユーザー定義の simpleType あるいは complexType に組み込まれているかどうかにより、 XmlSchemaDatatypeXmlSchemaSimpleType 、または XmlSchemaComplexType 。現在のノードにスキーマ型がない場合は、 null 参照 (Visual Basic では Nothing) 。

注意    Close を呼び出した後、 SchemaType は Null を返します。

解説

ユーザーは、返された型をテストする必要があります。例

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 スキーマ (XSD) 検証が実行される場合は、 XmlSchemaType または XmlSchemaDatatype は、読み取られる現在の要素に対応します。DTD 検証が実行される場合、このプロパティは null 参照 (Visual Basic では Nothing) を返します。

現在の要素または属性が、min や max のように単純な型で特殊な検証制約を指定できる単純型の場合は、 XmlSchemaDatatype が返されます。

現在の要素または属性がユーザー定義の simpleType の場合は、 XmlSchemaSimpleType が返されます。

現在の要素がユーザー定義の complexType の場合は、 XmlSchemaComplexType が返されます。この型は属性で返すことができません。

メモ    ValidationType を ValidationType.None に設定した場合、スキーマまたは DTD からデータ型情報が提供されません。

使用例

[Visual Basic, C#, C++] XML ドキュメント内の各要素の型情報を表示する例を次に示します。

 
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++] この例では、次の入力ファイルを使用します。

[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] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

参照

XmlValidatingReader クラス | XmlValidatingReader メンバ | System.Xml 名前空間