XAttribute Class represents an XML attribute. : XAttribute « XML LINQ « C# / C Sharp






XAttribute Class represents an XML attribute.

 


using System;
using System.Text;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using System.Collections;
using System.Collections.Generic;

public class MainClass
{
    public static void Main()
    {
        StringBuilder output = new StringBuilder();
        XElement root;

        double dbl = 12.345;
        XAttribute[] attArray = {
            new XAttribute("Att4", 1),
            new XAttribute("Att5", 2),
            new XAttribute("Att6", 3)
        };
        DateTime dt = new DateTime(2006, 10, 6, 12, 30, 00);
        root = new XElement("Root",new XAttribute("Att1", "Some text"),
            new XAttribute("Att2", dbl),
            new XAttribute("Att3", dt),
            attArray
        );
        output.Append(root + Environment.NewLine);
        Console.WriteLine(output.ToString());
    }
}

   
  








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.Create a new instance of the XAttribute class from the specified name and value.
5.Create XAttribute with namespace
6.Add XAttribute array to XElement
7.XAttribute.IsNamespaceDeclaration Property tells if this attribute is a namespace declaration.
8.XAttribute.Name Property returns the expanded name of this attribute.
9.XAttribute.NextAttribute Property returns the next attribute of the parent element.
10.XAttribute.NodeType Property returns the node type for this node.
11.XAttribute.PreviousAttribute Property returns the previous attribute of the parent element.
12.XAttribute.Remove removes this attribute from its parent element.
13.XAttribute.SetValue sets the value of this attribute.
14.XAttribute.ToString converts the current XAttribute object to a string representation.
15.XAttribute.Value gets or sets the value of this attribute.
16.It is easier to use casting when the attribute might or might not exist.
17.Cast the value of this XAttribute to a Boolean.
18.Cast the value of this XAttribute to a DateTime.