Cast the value of XAttribute to a DateTime in CSharp

Description

The following code shows how to cast the value of XAttribute to a DateTime.

Example


using System;// w  ww. j  a  v a2s.c  om
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using System.Collections;
using System.Collections.Generic;

public class MainClass{
   public static void Main(){
        XElement root = new XElement("Root",
            new XAttribute("Att", new DateTime(2010, 10, 6, 12, 30, 0))
        );
        Console.WriteLine(root);
        
        DateTime dt = (DateTime)root.Attribute("Att");
        Console.WriteLine("dt={0}", dt);
        
        XAttribute dtAtt = new XAttribute("OrderDate", "October 6, 2006");
        Console.WriteLine(dtAtt);
        DateTime orderDate = (DateTime)dtAtt;
        Console.WriteLine("OrderDate={0:d}", orderDate);

   }
}




















Home »
  C# Tutorial »
    XML »




Load Parse
Document
Element
Attribute
Namespace
Query
Save
Schema
Style