Mega Code Archive

 
Categories / VB.Net / XML
 

Validates XmlDocument against the XML Schema Definition Language (XSD) schemas

Imports System Imports System.Xml Imports System.Xml.Schema Imports System.Xml.XPath Class XPathValidation     Shared Sub Main()             Dim settings As XmlReaderSettings = New XmlReaderSettings()             settings.Schemas.Add("http://www.domain.com/books", "domainBooks.xsd")             settings.ValidationType = ValidationType.Schema             Dim reader As XmlReader = XmlReader.Create("domainBooks.xml", settings)             Dim document As XmlDocument = New XmlDocument()             document.Load(reader)             Dim eventHandler As ValidationEventHandler = New ValidationEventHandler(AddressOf ValidationEventHandler)             document.Validate(eventHandler)             Dim navigator As XPathNavigator = document.CreateNavigator()             navigator.MoveToFollowing("price", "http://www.domain.com/books")             Dim writer As XmlWriter = navigator.InsertAfter()             writer.WriteStartElement("anotherNode", "http://www.domain.com/books")             writer.WriteEndElement()             writer.Close()             document.Validate(eventHandler)     End Sub     Shared Sub ValidationEventHandler(ByVal sender As Object, ByVal e As ValidationEventArgs)         Select Case e.Severity             Case XmlSeverityType.Error                 Console.WriteLine("Error: {0}", e.Message)             Case XmlSeverityType.Warning                 Console.WriteLine("Warning {0}", e.Message)         End Select     End Sub End Class