Click here to Skip to main content
15,880,469 members
Articles / Web Development / ASP.NET
Article

Project file which will validate for XML with XSD

Rate me:
Please Sign up or sign in to vote.
3.77/5 (11 votes)
6 Apr 2004 61.4K   481   17   8
Validating XML for the given XSD. Output is displayed in a textarea.

Introduction

In this project, you can select your XML file and corresponding XSD file. And you can do a validation.

Error on validation will be shown in the text area below the form. Here is the function which validates the XML:

VB
Private Sub XMLvalidate_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles XMLvalidate.Click
        If Trim(txtXMLname.Text) = "" Or Trim(txtDTDname.Text) = "" Then
            MsgBox("Please select XML/XSD file", MsgBoxStyle.Information)
            Exit Sub
        End If
        Dim tr As XmlTextReader = New XmlTextReader(strXMLFileName)
        Dim sc As XmlSchemaCollection = New XmlSchemaCollection()
        Dim vr As XmlValidatingReader = New XmlValidatingReader(tr)
        Try
            txtMsg.Text = ""
            sc.Add(Nothing, strDTDFileName)
            vr.ValidationType = ValidationType.Schema
            vr.Schemas.Add(sc)
            AddHandler vr.ValidationEventHandler, AddressOf ValidationCallBack
            While (vr.Read())
            End While
            txtMsg.Text = txtMsg.Text & "Validation Success"
            txtMsg.Text = txtMsg.Text & vbCrLf
        Catch ee As Exception
            txtMsg.Text = ee.Message & ":" & ee.Source
        End Try
    End Sub

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Team Leader
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalgood application and test code Pin
Donsw24-Aug-09 6:59
Donsw24-Aug-09 6:59 
GeneralValidating when the XML refuses to refence your XSD Pin
scott.leckie11-Jul-09 15:28
scott.leckie11-Jul-09 15:28 
Generalminoccurs validation Pin
RHBKV21-Nov-07 18:22
RHBKV21-Nov-07 18:22 
GeneralRe: minoccurs validation Pin
Anil Gopalakrishnan22-Nov-07 17:56
Anil Gopalakrishnan22-Nov-07 17:56 
QuestionRe: minoccurs validation Pin
RHBKV27-Nov-07 23:40
RHBKV27-Nov-07 23:40 
Hi,
i have used your code like this
AddHandler vr.ValidationEventHandler, AddressOf ValidationEntHandler
While (vr.Read())
End While
Private Sub ValidationEntHandler(ByVal sender As Object, ByVal e As ValidationEventArgs)
Select Case e.Severity
Case XmlSeverityType.Error
strErrorDesc & = "Error: " & e.Message()
Case XmlSeverityType.Warning
strErrorDesc & = "Warning: " & e.Message()
End Select
end sub

suppose if i used <xsd:pattern> in my schema to check the datatype and value,
and any node value is mismatch it captured in "ValidationEntHandler" method. but suppose if miss any two nodes itself, only one node error only captured.
eg xml:
<node>
<node1>node1 value </node1>
<node4>node4 value </node4>
<node5>node5 value </node5>
</node>
xsd:
<xsd:sequence>
<xsd:element name="node1" minOccurs="1" type="xsd:string">
<xsd:element name="node2" minOccurs="1" type="xsd:string">
<xsd:element name="node3" minOccurs="1" type="xsd:string">
<xsd:element name="node4" minOccurs="1" type="xsd:string">
<xsd:element name="node5" minOccurs="1" type="xsd:string">
</xsd:sequence>
in this eg xml i missed two nodes (node2 ,node3).
Reader throw the error while parent node(node 4) reads. after that it moves to the value(node4 value) for that node. so node3 will be missed and wont come to the ValidationEntHandler method.
could you please help me how to solve this.
Thanks
GeneralNice app Pin
ceejeeb14-Aug-07 5:15
ceejeeb14-Aug-07 5:15 
GeneralRe: Nice app Pin
Anil Gopalakrishnan16-Aug-07 19:55
Anil Gopalakrishnan16-Aug-07 19:55 
GeneralNice little app... Pin
InteractiveLogic22-Jul-04 7:31
InteractiveLogic22-Jul-04 7:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.