Cast the value of this XAttribute to a DateTime. : XAttribute « XML LINQ « C# / C Sharp






Cast the value of this XAttribute to a DateTime.

 

using System;
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);

   }
}

   
  








Related examples in the same category

1.Using the LINQ to XML API to Create an XML Document
2.Creating an Attribute with Functional Construction
3.Creating an Attribute and Adding It to Its Element
4.XAttribute Class represents an XML attribute.
5.Create a new instance of the XAttribute class from the specified name and value.
6.Create XAttribute with namespace
7.Add XAttribute array to XElement
8.XAttribute.IsNamespaceDeclaration Property tells if this attribute is a namespace declaration.
9.XAttribute.Name Property returns the expanded name of this attribute.
10.XAttribute.NextAttribute Property returns the next attribute of the parent element.
11.XAttribute.NodeType Property returns the node type for this node.
12.XAttribute.PreviousAttribute Property returns the previous attribute of the parent element.
13.XAttribute.Remove removes this attribute from its parent element.
14.XAttribute.SetValue sets the value of this attribute.
15.XAttribute.ToString converts the current XAttribute object to a string representation.
16.XAttribute.Value gets or sets the value of this attribute.
17.It is easier to use casting when the attribute might or might not exist.
18.Cast the value of this XAttribute to a Boolean.