I am using the following code to validate my xml source.
Public Shared Sub ValidateSuppliedXMLData(ByVal _xDoc As MemoryStream, ByVal _SchemaFileName As String)
Try
Dim _xmlSetting As XmlReaderSettings = New XmlReaderSettings()
_xmlSetting.Schemas.Add("urn:dis:supplierengagement:v02", _SchemaFileName)
_xmlSetting.ValidationType = ValidationType.Schema
Dim _xmlReader As XmlReader = XmlReader.Create(_xDoc, _xmlSetting)
Dim _doc As XmlDocument = New XmlDocument()
Dim _xmlHandler As ValidationEventHandler = New ValidationEventHandler(AddressOf ValidationEventHandler)
_doc.Load(_xmlReader)
_doc.Validate(_xmlHandler)
_xmlReader.Close()
Catch xm As XmlSchemaValidationException
Throw New Exception(xm.Message)
Catch ex As Exception
Throw New Exception(ex.Message)
End Try
End Sub
Private Shared Sub ValidationEventHandler(ByVal sender As Object, ByVal args As ValidationEventArgs)
Select Case args.Severity
Case XmlSeverityType.Error
' Oh dear
Throw New Exception(args.Exception.Message)
Case XmlSeverityType.Warning
' Not so bad
Throw New Exception(args.Exception.Message)
End Select
End Sub
When I introduce an validation error in the code, I wish to recover the xpath to the faulting element.
The code above stops when the line
_doc.Load(_xmlReader)
is executed, and fails with a validation error within the
Catch xm As XmlSchemaValidationException
Throw New Exception(xm.Message)
code block.
What am I doing wrong and will this product the xpath to the error?
If you want to validate your xml source, I suggest you using XmlValidatingReader to achieve it.
There have a sample:
Public Shared Sub Main()
Dim tr As New XmlTextReader("HeadCount.xml")
Dim vr As New XmlValidatingReader(tr)
vr.ValidationType = ValidationType.Schema
AddHandler vr.ValidationEventHandler, AddressOf ValidationHandler
While vr.Read()
PrintTypeInfo(vr)
If vr.NodeType = XmlNodeType.Element Then
While vr.MoveToNextAttribute()
PrintTypeInfo(vr)
End While
End If
End While
Console.WriteLine("Validation finished")
End Sub
' Main
Public Shared Sub PrintTypeInfo(vr As XmlValidatingReader)
If Not (vr.SchemaType Is Nothing) Then
If TypeOf vr.SchemaType Is XmlSchemaDatatype Or TypeOf vr.SchemaType Is XmlSchemaSimpleType Then
Dim value As Object = vr.ReadTypedValue()
Console.WriteLine("{0}({1},{2}):{3}", vr.NodeType, vr.Name, value.GetType().Name, value)
Else
If TypeOf vr.SchemaType Is XmlSchemaComplexType Then
Dim sct As XmlSchemaComplexType = CType(vr.SchemaType, XmlSchemaComplexType)
Console.WriteLine("{0}({1},{2})", vr.NodeType, vr.Name, sct.Name)
End If
End If
End If
End Sub
' PrintTypeInfo
Public Shared Sub ValidationHandler(sender As Object, args As ValidationEventArgs)
Console.WriteLine("***Validation error")
Console.WriteLine("Severity:{0}", args.Severity)
Console.WriteLine("Message:{0}", args.Message)
End Sub
Member
6 Points
23 Posts
XML fails validation get Xpath to faulting element
Dec 11, 2013 06:53 AM|gilesrpa|LINK
I am using the following code to validate my xml source.
When I introduce an validation error in the code, I wish to recover the xpath to the faulting element.
The code above stops when the line
is executed, and fails with a validation error within the
code block.
What am I doing wrong and will this product the xpath to the error?
XPATH xml Schema validation
Star
12777 Points
1635 Posts
Re: XML fails validation get Xpath to faulting element
Dec 12, 2013 02:52 AM|Terry Guo - MSFT|LINK
Hi gilesrpa,
If you want to validate your xml source, I suggest you using XmlValidatingReader to achieve it.
There have a sample:
More information please refer to:
http://msdn.microsoft.com/en-us/library/thydszwy(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2
Hope it helps.
Best Regards,
Terry Guo
XPATH xml Schema validation