I am validating and XML file against a Schema using the following code.
Public Sub LoadXml(xmlFilePath As String, xsdFilePath As String)
Dim doc As New XmlDocument()
doc.Load(xmlFilePath)
doc.Schemas.Add(Nothing, xsdFilePath)
Dim errorBuilder As New XmlValidationErrorBuilder()
doc.Validate(New ValidationEventHandler(AddressOf errorBuilder.ValidationEventHandler))
Dim errorsText As List(Of String) = errorBuilder.GetErrors()
End Sub
End Class
Public Class XmlValidationErrorBuilder
Private _errors As New List(Of ValidationEventArgs)()
Public Sub ValidationEventHandler(ByVal sender As Object, ByVal args As ValidationEventArgs)
If args.Severity = XmlSeverityType.Error Then
_errors.Add(args)
End If
End Sub
Public Function GetErrors() As List(Of String)
If _errors.Count <> 0 Then
Dim ErrorList As New List(Of String)
For i As Integer = 0 To _errors.Count - 1
Dim Message As String = _errors(i).Message
Dim LineNumber As String = _errors(i).Exception.LineNumber
Dim LinePosition As String = _errors(i).Exception.LinePosition
Dim combineString = Message & "|" & LineNumber & "|" & LinePosition
ErrorList.Add(combineString)
Next
Return ErrorList
Else
Return Nothing
End If
End Function
Any validation errors are stored in a List(Of ValidationEventArgs) and are later processed by the GetErrors function. My problem is that Exception.LineNumber and Exception.LinePosition always returns 0. How can I get the line number and line position of the error?