Validating XML using DTD RRS feed

  • Question

  • User-1895243249 posted

    Howdy all,

    Got myself a bit of a problem. I'm trying to validate an XML file using a DTD using the following code from http://msdn.microsoft.com/en-us/library/z2adhb2f.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1

        Private Sub BTN_ValidateXMLdoc_Click(sender As Object, e As EventArgs) Handles BTN_ValidateXMLdoc.Click
            Dim JobNumber As String = Me.HNF_JobName.Value
            Dim XMLPath As String = "N:\" & JobNumber & "\" & JobNumber & ".xml"
            ' Set the validation settings.
            Dim settings As XmlReaderSettings = New XmlReaderSettings()
            settings.DtdProcessing = DtdProcessing.Parse
            settings.ValidationType = ValidationType.DTD
            settings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings
            settings.ValidationFlags = XmlSchemaValidationFlags.ProcessInlineSchema
            AddHandler settings.ValidationEventHandler, AddressOf ValidationCallBack
            ' Create the XmlReader object.
            Dim reader As XmlReader = XmlReader.Create(XMLPath, settings)
            ' Parse the file. 
            While reader.Read()
            End While
        End Sub
      ' Display any validation errors.
        Private Shared Sub ValidationCallBack(sender As Object, e As ValidationEventArgs)
            MsgBox(String.Format("Validation Error: {0}", e.Message), MsgBoxStyle.OkOnly)
        End Sub

    Now, for some reason, and I have no idea what the reason is (hence me posting here) if an error occurs while reader.Read(), instead of the ValidationCallBack sub being called, it throws an exception error. Now I could be wrong but if there is a validation error while the reader.read() is executing, shouldn't the ValidationEventHandler be fired?

    Friday, August 30, 2013 3:27 AM


  • User-933407369 posted

    hi cre8tive,

    According to your description, I understand you want to throws an exception error without the ValidationCallBack.

    I suppose that you could wrap the logic within a try-catch and if it fails target  to use “Try …Catch...End Try”below:     

                 'Your key code (process events)
            Catch ex As Exception
                Response.Write("<script>alert('" + ex.Message + "')</script>")
            End Try

    If you need more assistance, please let me know.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 2, 2013 5:36 AM