Francesco Geri

Il blog di Francesco Geri
posts - 94, comments - 165, trackbacks - 2

Validazione di un file XML dallo schema XSD

Liberamente tratto da MSDN riporto il codice per la validazione di un file XML da uno schema XSD.

 

   1:  Imports System
   2:  Imports System.Xml
   3:  Imports System.Xml.Schema
   4:  Imports System.IO
   5:   
   6:  Public Class Sample
   7:   
   8:     Public Shared Sub Main()
   9:   
  10:        ' Create the XmlSchemaSet class.
  11:        Dim sc As XmlSchemaSet = New XmlSchemaSet()
  12:   
  13:        ' Add the schema to the collection.
  14:        sc.Add("", "Schema.xsd")
  15:   
  16:        ' Set the validation settings.
  17:        Dim settings As XmlReaderSettings = New XmlReaderSettings()
  18:        settings.ValidationType = ValidationType.Schema
  19:        settings.Schemas = sc
  20:        settings.ValidationFlags = settings.ValidationFlags Or XmlSchemaValidationFlags.ReportValidationWarnings
  21:        AddHandler settings.ValidationEventHandler, AddressOf ValidationCallBack
  22:   
  23:        ' Create the XmlReader object.
  24:        Dim reader As XmlReader = XmlReader.Create("MyFile.xml", settings)
  25:   
  26:        ' Parse the file. 
  27:        While reader.Read()
  28:        End While
  29:   
  30:        Console.ReadKey()
  31:     End Sub
  32:   
  33:     ' Display any validation errors.
  34:     Private Shared Sub ValidationCallBack(ByVal sender As Object, ByVal e As ValidationEventArgs)
  35:        Console.WriteLine("Validation Error: {0}", e.Message)
  36:     End Sub
  37:  End Class

Print | posted on giovedì 25 ottobre 2007 22:50 | Filed Under [ Tips .Net XML ]

Powered by:
Powered By Subtext Powered By ASP.NET