lundi 11 mai 2015

Obsolete XML/XSD validation classes and functions

I'm trying to manipulate some code to first validate an XSD schema and then a XML file against that schema. I cannot cause the XML file to fail despite entering rogue values so I think that maybe an obsolete class and function are not functioning as they should (XmlValidatingReader & Compile). Can anyone suggest some code that would be fit for purpose here?

Private Function validatexml(ByVal infile As String) As Boolean
    'First we create the xmltextreader
    Dim xmlr As New XmlTextReader(infile)
    'Pass the xmltextreader into the xmlvalidatingreader
    'This will validate the xml doc with the schema file
    'The xml file itself points to the schema file
    Dim xmlvread As New XmlValidatingReader(xmlr)  'obsolete!!!!!

    'Set the validation event handler
    AddHandler xmlvread.ValidationEventHandler, AddressOf ValidationCallBack
    m_Success = True 'make sure to reset the success var

    'Read XML data
    While (xmlvread.Read)
    End While
    'Close the reader.
    xmlvread.Close()

    'The validationeventhandler is the only thing that would set m_Success to false
    Return m_Success
End Function


Private Sub ValidationCallBack(ByVal sender As Object, ByVal args As ValidationEventArgs)
    'Display the validation error.  This is only called on error
    m_Success = False 'Validation failed
    writertbox("Validation error: " + args.Message)
End Sub


Private Function validateSchema(ByVal infilename As String) As Boolean
    'this function will validate the schema file (xsd)
    Dim sr As StreamReader
    Dim myschema As XmlSchema
    m_Success = True
    Try
        sr = New StreamReader(infilename)
        myschema = XmlSchema.Read(sr, AddressOf ValidationCallBack)
        'This compile statement is what ususally catches the errors
        myschema.Compile(AddressOf ValidationCallBack) 'obsolete!!!!!
    Finally
        sr.Close()
    End Try
    Return m_Success
End Function

Aucun commentaire:

Enregistrer un commentaire