title: XDocument.Validate - Expected Data Type of error element
body: <p>I have a class which validates the supplied XML document against the supplied XSD. In the class I call the <code>XDocument.Validate</code> method to perform validation, and getting the following error:</p>
  
  <blockquote>
    <p>The '<a href="http://www.example.com/enrollrequest1:requested-payment-date" rel="nofollow">http://www.example.com/enrollrequest1:requested-payment-date</a>'
    element is invalid - The value '2015-05-28T00:00:00' is invalid
    according to its datatype '<a href="http://www.w3.org/2001/XMLSchema:date" rel="nofollow">http://www.w3.org/2001/XMLSchema:date</a>' -
    The string '2015-05-28T00:00:00' is not a valid XsdDateTime value.</p>
  </blockquote>
  
  <p>The value for element has been set from a <code>.NET DateTime</code> variable, which ultimately sets it with the time part included, since there is no equivalent of xs:date type in .NET.</p>
  
  <p>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 <code>XElemet.SetValue(value)</code> method to set it.</p>
  
  <p>Also, the XSD file is out of my control. So modifying the XSD is not an option.</p>
  
  <p>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 <code>xs:date</code> (and not <code>xs:datetime</code>), I can simply typecast the incoming value. </p>
  
  <p>Here is my validator class, if this helps:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Here is the function that is sets the xml node values. </p>
  
  <pre><code>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 &gt; 0 Then
          'Error Logging code goes here...
          Return False
      End If
      Return True
  End Function
  </code></pre>