Validating XML documents against Schemas : XML Schema « XML « VB.Net






Validating XML documents against Schemas

  
Imports System.Xml
Imports System.Xml.Schema

Public Class MainClass
   Shared Dim valid As Boolean ' validation result

   Shared Sub Main()
       Dim schemas As XmlSchemaCollection ' Schemas

      valid = True ' assume document is valid

      ' get Schema(s) for validation
      schemas = New XmlSchemaCollection()
      schemas.Add("book", "book.xdr")
      ' get XML document
      Dim reader As XmlTextReader = _
         New XmlTextReader("document.xml")

      ' get validator
      Dim validator As XmlValidatingReader = _
         New XmlValidatingReader(reader)

      ' assign Schema(s)
      validator.Schemas.Add(schemas)

      ' Microsoft XDR validation
      validator.ValidationType = ValidationType.XDR

      ' register event handler for validation error(s)
      AddHandler validator.ValidationEventHandler, _
         AddressOf ValidationError

      ' validate document node-by-node
      While validator.Read

         ' empty body
      End While

      ' check validation result
      If valid Then
         Console.WriteLine("Document is valid")
      End If

      valid = True ' reset variable

      validator.Close()
   End Sub 

   ' event handler for validation error
   Shared Private Sub ValidationError(ByVal sender As Object, _
      ByVal arguments As ValidationEventArgs)

      Console.WriteLine(arguments.Message)
      valid = False ' validation failed
   End Sub ' ValidationError

End Class
 


           
         
    
  








Related examples in the same category

1.Compare the XML document generated from DataSet and Original one
2.Read XML without schema and output its structureRead XML without schema and output its structure
3.Output data relation from XML document
4.Generate XML Schema from XML document
5.XMl schema generatorXMl schema generator
6.Validating a Document using the Validate Method
7.XDocument XML schema validation
8.XmlSchema represents an XML Schema
9.Add XmlSchemaElement to XmlSchema
10.Create XmlSchemaComplexType
11.XmlSchemaAll Class represents the World Wide Web Consortium (W3C) all element (compositor).
12.XmlSchemaAny Class represents the World Wide Web Consortium (W3C) any element.
13.XmlSchemaAnyAttribute Class represents the World Wide Web Consortium (W3C) anyAttribute element.
14.XmlSchemaAppInfo Class represents the World Wide Web Consortium (W3C) appinfo element.
15.XmlSchemaAttribute Class represents attribute element from XML Schema
16.XmlSchemaAttributeGroup Class represents attributeGroup element from XML Schema
17.XmlSchemaChoice Class represents choice element from XML Schema
18.XmlSchemaComplexContent represents complexContent element from XML Schema
19.XmlSchemaElement MinOccurs MaxOccursString
20.XmlSchemaComplexType Class represents complexType element from XML Schema
21.XmlSchemaDocumentation Class represents documentation element from XML Schema
22.XmlSchemaEnumerationFacet Class represents enumeration facet from XML Schema
23.XmlSchemaException Class returns detailed information about the schema exception.
24.XmlSchemaFractionDigitsFacet Class sets a restriction for the fraction value of a simpleType element
25.XmlSchemaGroup Class represents group element from XML Schema
26.XmlSchemaInclude Class represents include element from XML Schema
27.XmlSchemaLengthFacet Class represents the length facet from XML Schema
28.XmlSchemaMaxExclusiveFacet Class represents the maxExclusive element from XML Schema
29.XmlSchemaNotation represents the notation element from XML Schema
30.XmlSchemaSimpleContent Class represents simpleContent element from XML Schema
31.XmlSchemaSimpleTypeUnion Class represents the union element for simple types from XML Schema
32.XmlSchemaWhiteSpaceFacet Class tepresents the World Wide Web Consortium (W3C) whiteSpace facet.
33.XmlSchemaAnnotation Class represents the World Wide Web Consortium (W3C) annotation element.
34.XmlSchemaAttribute Class represents the attribute element from the XML Schema
35.XmlSchemaAttributeGroup Class represents the attributeGroup element from the XML Schema
36.XmlSchemaComplexContent Class represents complexContent element from XML Schema
37.Extensions.Validate validates that an XAttribute conforms to a specified XmlSchemaObject and an XmlSchemaSet.
38.Extensions.Validate validates that an XDocument conforms to an XSD in an XmlSchemaSet.
39.Validates that an XDocument conforms to an XSD in an XmlSchemaSet
40.Validates that an XElement sub-tree conforms to a specified XmlSchemaObject and an XmlSchemaSet.
41.XmlSchemaObject Class represents the root class for the Xml schema object model
42.XmlSchemaSet contains a cache of XML Schema definition language (XSD) schemas.
43.Create substitutionGroup