다음을 통해 공유


Accessing Schema Type Information

You can access schema type information using the SchemaType property. The SchemaType property returns either an XmlSchemaType or XmlSchemaDataType depending on whether the value is a built-in XML Schema definition language (XSD) type or a user-defined type (for example, a simpleType or a complexType).

Checking for the Returned Type

You can check for the returned type using the SchemaType property on the XmlValidatingReader class.

The following code example checks for the returned type.

Dim obj As object = vreader.SchemaType
if (obj is XmlSchemaType)
    XmlSchemaType st = obj.BaseSchemaType
if (obj is XmlSchemaDataType)
    XmlSchemaDatatype vt = obj.ValueType
[C#]
object obj=vreader.SchemaType;
if (obj is XmlSchemaType)
    XmlSchemaType st = obj.BaseSchemaType;
if (obj is XmlSchemaDataType)
    XmlSchemaDatatype vt = obj.ValueType;

For an example using the SchemaType property, see the XmlValidatingReader.SchemaType Property.

Validating Schemas

If you are performing XML Schema (XSD) or XML-Data Reduced (XDR) schema validation, the XmlSchemaType or XmlSchemaDataType corresponds to the current element that is being read.

If the current element or attribute is a simple type, an XmlSchemaDataType is returned. If the current element or attribute is a user-defined complex type, an XmlSchemaType is returned.

The following code example determines if the SchemaType property is an XmlSchemaComplexType. The value of the object is checked using the ReadTypedValue method and is written out to the console.

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
[C#]
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);
                    }
                }
            }

If you are performing document type definition (DTD) validation, the XmlSchemaType or XmlSchemaDataType returns a null reference.

When the Close method is called, this property returns to a null reference. A null reference can be returned if the current node has no schema type or if no schema is present.

Using the ReadTypedValue Method

The ReadTypedValue method reads and returns an object that corresponds to a .NET Framework typed object for a given XML Schema element. For example, if the type is defined as xs:int, the .NET Framework type Int32 is returned for that object.

The operation performed depends on the current node type and is based on the following rules:

  • If the node is an attribute node, the reader remains in the current position.
  • If the node is an element node, the reader is positioned on the EndElement reading CDATA, text, white space, significant white space, and comments.
  • If the type does not have a direction mapping (for example NMTOKENS), it is returned as a string.
  • If you set the ValidationType property to ValidationType.None, no data type information is provided for schemas or DTDs.

After the Close method is called, the ReadTypedValue method returns a null reference.

The following code example reads and returns the .NET Framework typed object.

Dim value as object = vr.ReadTypedValue()
[C#]
object value = vr.ReadTypedValue();

Validation Types

Data type information can be discovered for ValidationType property values set to ValidationType.Schema or ValidationType.XDR. No data type information is provided for ValidationType.DTD or ValidationType.None.

In the following code example, the first line sets the ValidationType property to find XML Schema (XSD) data types, and the second line sets the ValidationType property to find XDR data types.

vr.ValidationType = ValidationType.Schema
vr.ValidationType = ValidationType.XDR
[C#]
vr.ValidationType = ValidationType.Schema;
vr.ValidationType = ValidationType.XDR;

See Also

Validation of XML with Schemas | XmlValidatingReader.SchemaType Property | XmlValidatingReader.ReadTypedValue Method