Cast XElement to DateTime in CSharp

Description

The following code shows how to cast XElement to DateTime.

Example


using System;/*from   w ww  .j av a 2  s  .c om*/
using System.Linq;
using System.Xml.Linq;
using System.Collections;
using System.Collections.Generic;


public class MainClass{
   public static void Main(){

        XElement root = new XElement("Root", new DateTime(2010, 10, 6, 12, 30, 0));
        Console.WriteLine(root);
        
        DateTime dt = (DateTime)root;
        Console.WriteLine("dt={0}", dt);
        
        XElement dtElement = new XElement("OrderDate", "October 6, 2006");
        Console.WriteLine(dtElement);
        DateTime orderDate = (DateTime)dtElement;
        Console.WriteLine("orderDate={0:d}", orderDate);
    }
}




















Home »
  C# Tutorial »
    XML »




Load Parse
Document
Element
Attribute
Namespace
Query
Save
Schema
Style