Validates XmlDocument against the XML Schema Definition Language (XSD) schemas : XmlDocument « XML « VB.Net





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

   
  










Related examples in the same category

1.Create XmlDocument class.
2.XmlDocument.CloneNode
3.XmlDocument.CreateAttribute
4.XmlDocument.CreateCDataSection creates an XmlCDataSection containing the specified data.
5.XmlDocument.CreateComment creates an XmlComment containing the specified data.
6.XmlDocument.CreateDocumentFragment creates an XmlDocumentFragment.
7.XmlDocument.CreateDocumentType returns a new XmlDocumentType object.
8.XmlDocument.CreateElement creates an element with the specified name.
9.XmlDocument.CreateElement creates an element with the specified Prefix, LocalName, and NamespaceURI.
10.XmlDocument.CreateEntityReference creates an XmlEntityReference with the specified name.
11.Saves all the children of the XmlDocument node to the specified XmlWriter.
12.Saves the XmlDocument node to the specified XmlWriter.
13.Gets the XmlDocument to which this node belongs.
14.Set InnerText to a string that includes markup
15.Set InnerXml to a string that includes markup
16.Create a shallow clone.
17.Gets or sets a value indicating whether to preserve white space in element content.
18.InnerXml includes the markup of the element
19.Gets or sets the tag format of the element.