Share via


How to: Read Typed Data with No Schema Mapping

The following example uses the XmlReader to return a typed object. Because the format of the hire-date element follows the W3C rules for the xs:dateTime type, the ReadElementContentAsDateTime method is able to successfully convert it to a DateTime object, even though the data is untyped.

Example

The following example uses the ReadElementContentAsDateTime method to return the hire-date element as a DateTime object.

' Create an XmlReader object.
Using reader As XmlReader = XmlReader.Create("hireDate_1.xml")
  ' Move to the hire-date element.
  reader.MoveToContent()
  reader.ReadToDescendant("hire-date")

  ' Return the hire-date as a DateTime object.
  Dim hireDate As DateTime = reader.ReadElementContentAsDateTime()
  Console.WriteLine("Six Month Review Date: {0}", hireDate.AddMonths(6))
End Using
// Create an XmlReader object.
using (XmlReader reader = XmlReader.Create("hireDate_1.xml")) {
  // Move to the hire-date element.
  reader.MoveToContent();
  reader.ReadToDescendant("hire-date");

  // Return the hire-date as a DateTime object.
  DateTime hireDate = reader.ReadElementContentAsDateTime();
  Console.WriteLine("Six Month Review Date: {0}", hireDate.AddMonths(6));
}

Input

The example uses the hireDate_1.xml file as input.

hireDate_1.xml

<employee xmlns="urn:empl-hire">
    <ID>12365</ID>
    <hire-date>2003-01-08T15:00:00-00:00</hire-date>
    <title>Accountant</title>
</employee>

Output

Six Month Review Date:  7/8/2003 8:00:00 AM

See Also

Concepts

Reading XML with the XmlReader

Reading Typed Data