VB.NET code to validate xml against xsd file
Hi all
Does anyone have any code which shows how i can use the system.xml objects to validate an xml file against its schema file (xsd) file. Need the code in vb.net 2.0. And need something wich will give me alot of error information for example what node is incorrect and data type and length errors.
Cheers
ragioli:afrog:
Re: VB.NET code to validate xml against xsd file
untested, but try this code:
Code:
Imports System
Imports System.Xml
Imports System.Xml.Schema
Imports System.Xml.XPath
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim myDocument As New XmlDocument
myDocument.Load("C:\somefile.xml")
myDocument.Schemas.Add("namespace here or empty string", "C:\someschema.xsd")
Dim eventHandler As ValidationEventHandler = New ValidationEventHandler(AddressOf ValidationEventHandler)
myDocument.Validate(eventHandler)
End Sub
Private Sub ValidationEventHandler(ByVal sender As Object, ByVal e As ValidationEventArgs)
Select Case e.Severity
Case XmlSeverityType.Error
Debug.WriteLine("Error: {0}", e.Message)
Case XmlSeverityType.Warning
Debug.WriteLine("Warning {0}", e.Message)
End Select
End Sub
End Class
Re: VB.NET code to validate xml against xsd file
Thank you very much that worked a treat :-) :thumb: :bigyello:
Re: VB.NET code to validate xml against xsd file
Just used this, worked perfect! Thanks.