ReadContentAsDateTime Problem

Joined
Jun 18, 2006
Messages
23
Programming Experience
3-5
The xml node supports ReadContentAsDateTime method, according to intellisense, but it throws and exception when I use it:

VB.NET:
Try
Dim reader As New XmlTextReader(strfilename2)
reader.WhitespaceHandling = WhitespaceHandling.None
While reader.Read()
 
If reader.NodeType = XmlNodeType.XmlDeclaration Then
 
ElseIf reader.NodeType = XmlNodeType.Attribute Then
 
ElseIf reader.NodeType = XmlNodeType.Element Or reader.NodeType = XmlNodeType.Text Then
If reader.Name = "filename" Then
strFileName = reader.ReadString
ElseIf reader.Name = "category" Then
strCategory = reader.ReadString
ElseIf reader.Name = "title" Then
strTitle = reader.ReadString
ElseIf reader.Name = "text" Then
strMainText = reader.ReadString
[COLOR=red]ElseIf reader.Name = "dateOfCreation" Then[/COLOR]
[COLOR=red]dateOfCreation = reader.ReadContentAsDateTime[/COLOR]
[COLOR=red]ElseIf reader.Name = "dateModified" Then[/COLOR]
[COLOR=red]dateModified = reader.ReadContentAsDateTime[/COLOR]
End If
End If
 
End While
reader.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
Exit Sub
End Try
Marked two lines cause an exception. What am I doing wrong?

It tells me to use ReadElementContentAs instead, but Intellisense doesn't have a problem when I write the code.

How do I use ReadElementContentAs?
 
Last edited by a moderator:
What is the exception?
Can you post a sample xml data file to process by your method that cause this problem to happen?
It may be that the date you are trying to read is not in a format compatible with date formats of your culture settings.
 
VB.NET:
<?xml version="1.0" encoding="utf-8" ?>
<note>
  <category>Important</category>
  <title>Data</title>
  <text>Data to be read.</text>
  <dateOfCreation>1/5/2007 5:55:29 PM</dateOfCreation>
  <dateModified>1/5/2007 5:57:29 PM</dateModified>
</note>
The error message that shows in my messagebox is: "The ReadContentAsDateTime method is not supported on node type Element. If you want to read typed content of an element, use
the ReadElementContentAs method. Line 6, position 4."

And, in the Imediate Window: "A first chance exception of type 'System.InvalidOperationException' occurred in System.Xml.dll"

dateOfCreation and dateModified were both first declared as type Date, but changing it to type DateTime (which is not supported by Intellisense, btw) does not fix the problem.

The other data seems to be read fine, the dates are the only problems.
 
Ok, same as all the ReadContentAs.... methods you also got all the ReadElementContentAs.... methods for use with Element nodes. So the error message means to tell you can use the ReadElementContentAsDateTime method instead of the ReadContentAsDateTime method.

But it still won't work in this case, not with the date format you posted at least, because it have to conform to W3C standard datetime format as noted in documentation. (That's how I understand it, and trying the method on different formatted datetimes gives format error.) Reading element as string to parse with Date.Parse or TryParse should work. (did here)
 
Back
Top