I have a class which validates the supplied XML document against the supplied XSD. In the class I call the XDocument.Validate
method to perform validation, and getting the following error:
The 'http://www.example.com/enrollrequest1:requested-payment-date' element is invalid - The value '2015-05-28T00:00:00' is invalid according to its datatype 'http://www.w3.org/2001/XMLSchema:date' - The string '2015-05-28T00:00:00' is not a valid XsdDateTime value.
The value for element has been set from a .NET DateTime
variable, which ultimately sets it with the time part included, since there is no equivalent of xs:date type in .NET.
The values for the elements are being set from a generic module, so I can't pick and choose elements and customize setting their values. The developer sends me value in a .NET DateTime type, which my program in turn calls the XElemet.SetValue(value)
method to set it.
Also, the XSD file is out of my control. So modifying the XSD is not an option.
Is there a way to know what is the expected type of the XElement that caused the error?
Once I know it, I can just typecast or customize my code accordingly. So for example in this case, if I know that the expected type is xs:date
(and not xs:datetime
), I can simply typecast the incoming value.
Here is my validator class, if this helps:
Option Strict On
Imports System.XML.Schema
Public Class XmlSchemaValidator
Public ReadOnly Errors As New List(Of String)
Private XDoc As XDocument
Private Schemas As XmlSchemaSet
Public Sub New(ByVal doc As XDocument, ByVal schemaUri As String, ByVal targetNamespace As String)
Me.XDoc = doc
Me.Schemas = New XmlSchemaSet
Me.Schemas.Add(targetNamespace, schemaUri)
End Sub
Public Sub Validate()
Errors.Clear()
XDoc.Validate(Schemas, AddressOf XsdErrors)
End Sub
Private Sub XsdErrors(ByVal sender As Object, ByVal e As ValidationEventArgs)
Errors.Add (e.Message)
End Sub
End Class
Here is the function that is sets the xml node values.
Function SetValue(ByVal xmlDoc As XDocument, ByVal keyValues As Dictionary(Of String, Object)) As Boolean
'' set values
For Each kvp In keyValues
Dim xe As XElement = xmlDoc.Root.XPathSelectElement(kvp.Key)
''-- this is buggy implementation for handling xs:date vs xs:datetime that needs to be corrected...
'If TypeOf kvp.Value Is DateTime AndAlso DirectCast(kvp.Value, DateTime).TimeOfDay = TimeSpan.Zero Then
' xe.SetValue(DirectCast(kvp.Value, DateTime).ToString("yyyy-MM-dd"))
'Else
xe.SetValue(kvp.Value)
'End If
Next
'' validate final document
Dim schemaValidator As New XmlSchemaValidator(xmlDoc, schemaFile, "")
schemaValidator.Validate()
If schemaValidator.Errors.Count > 0 Then
'Error Logging code goes here...
Return False
End If
Return True
End Function
System.Date
class. – Scullionxs:date
orxs:time
orxs:datetime
, irrespective of what value is set for the element. The rest I can handle appropriately. – PontificalXElement.SetValue(value.ToString("yyyy-MM-dd"))
. – Pontificalhttp://www.w3.org/2001/XMLSchema:date
meaning that the expected type is exactlyxs:date
. So why don't you get the expected type from the validation error message? – TrymaDateTime
value correctly? Maybe useXmlConvert.ToString(dateTime, "yyyy-MM-dd")
. – ScullionXmlConvert.ToString(dateTime, "yyyy-MM-dd")
orXmlConvert.ToString(dateTime, "yyyy-MM-ddThh:mm:ss")
will not throw error when validated against the xsd file? – PontificalSystem.Date
andSystem.TimeOfDay
data types that usedxs:date
andxs:time
appropriately, such as these ones? – Quemoy