Results 1 to 4 of 4

Thread: VB.NET code to validate xml against xsd file

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Location
    England
    Posts
    64

    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

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Location
    England
    Posts
    64

    Re: VB.NET code to validate xml against xsd file

    Thank you very much that worked a treat :-)

  4. #4
    New Member
    Join Date
    Jan 2016
    Posts
    1

    Re: VB.NET code to validate xml against xsd file

    Just used this, worked perfect! Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width