共用方式為


使用內嵌 XML 結構描述 (XSD) 進行驗證

您可以使用 XmlValidatingReader 來根據內嵌 XML 結構描述定義語言 (XSD) 結構描述進行驗證。

Note注意事項

XmlValidatingReader 類別在 Microsoft .NET Framework 2.0 版本 中已過時。您可以使用 XmlReaderSettings 類別和 Create 方法,來建立驗證 XmlReader 執行個體。如需詳細資訊,請參閱使用 XmlReader 驗證 XML 資料

範例

下列程式碼範例會建立 XmlValidatingReader,其會使用 XmlTextReader。輸入檔 HeadCount.xml 會對內嵌的 XML 結構描述進行驗證。

Note注意事項

由於內嵌結構描述是以根項目的項目子系出現,因此當執行內嵌結構描述驗證時,無法驗證根項目。如果 ValidationType 屬性設為 Schema,則 XmlValidatingReader 會針對根項目擲回警告。

Imports System
Imports System.IO
Imports System.Xml
Imports System.Xml.Schema


Namespace ValidationSample
    
   Class Sample
      Private Shared _ValidationErrorsCount As Integer = 0
      
      
      Public Shared Sub Main()
         Dim stream As New FileStream("HeadCount.xml", FileMode.Open)
         Dim vr As New XmlValidatingReader(stream, XmlNodeType.Element, Nothing)
         
         vr.ValidationType = ValidationType.Schema
         AddHandler vr.ValidationEventHandler, AddressOf ValidationHandler
         
         While vr.Read()
         End While
         Console.WriteLine("Validation finished: {0} validation errors", _ValidationErrorsCount)
      End Sub
      ' Main
      
      
      Public Shared Sub ValidationHandler(sender As Object, args As ValidationEventArgs)
         Console.WriteLine("***Validation error")
         Console.WriteLine("Severity:{0}", args.Severity)
         Console.WriteLine("Message:{0}", args.Message)
         _ValidationErrorsCount += 1
      End Sub
      ' ValidationHandler
   End Class
   ' Sample
End Namespace 
' ValidationSample
using System;
using System.IO;
using System.Xml;
using System.Xml.Schema;

namespace ValidationSample
{
   class Sample
   {
      static int _ValidationErrorsCount = 0;

      public static void Main()
      {
         FileStream stream = new FileStream("HeadCount.xml", FileMode.Open);
         XmlValidatingReader vr = new XmlValidatingReader(stream, XmlNodeType.Element, null);

         vr.ValidationType = ValidationType.Schema;
         vr.ValidationEventHandler += new ValidationEventHandler (ValidationHandler);

         while(vr.Read());
         Console.WriteLine("Validation finished: {0} validation errors", _ValidationErrorsCount);
      }

      public static void ValidationHandler(object sender, ValidationEventArgs args)
      {
         Console.WriteLine("***Validation error");
         Console.WriteLine("\tSeverity:{0}", args.Severity);
         Console.WriteLine("\tMessage  :{0}", args.Message);
         _ValidationErrorsCount++;
      }
   }
}

以下列出了要進行驗證的輸入檔 (HeadCount.xml) 內容。

<root>
<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'
            xmlns='xsdHeadCount'
            targetNamespace='xsdHeadCount'>
    <xs:element name='HeadCount'>
        <xs:complexType>
            <xs:sequence>
                <xs:element name='Name' type='xs:string' maxOccurs='unbounded'/>
            </xs:sequence>
            <xs:attribute name='division' type='xs:string' use='optional' default='QA'/>
        </xs:complexType>
    </xs:element>
</xs:schema>
<hc:HeadCount xmlns:hc='xsdHeadCount'>
    <Name>Waldo Pepper</Name>
    <Name>Red Pepper</Name>
</hc:HeadCount>
</root>

下列程式碼範例會建立 XmlValidatingReader,其會使用 XmlTextReader。輸入檔 Sample5.xml 會對內嵌的 XML 結構描述進行驗證。

Dim tr As New XmlTextReader("Sample5.xml")
Dim vr As New XmlValidatingReader(tr)
vr.ValidationType = ValidationType.Schema
AddHandler vr.ValidationEventHandler, AddressOf ValidationCallBack
While vr.Read()
   Console.WriteLine("NodeType: {0} NodeName: {1}", vr.NodeType, vr.Name)
End While
XmlTextReader tr = new XmlTextReader("Sample5.xml");
XmlValidatingReader vr = new XmlValidatingReader(tr);
vr.ValidationType = ValidationType.Schema;
vr.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
while(vr.Read()) {
    Console.WriteLine("NodeType: {0} NodeName: {1}", vr.NodeType, vr.Name);
    }

以下列出了要進行驗證輸入檔 (Sample5.xml) 的內容。

<test>
  <schema targetNamespace='test' xmlns='http://www.w3.org/2001/XMLSchema' > 
    <element name='zip' type='positiveInteger'/> 
  </schema>

  <t:zip   xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'  xmlns:t='test'>
    123
  </t:zip>
</test>

請參閱

概念

使用 XmlReader 讀取 XML

其他資源

使用 XmlReader 類別