.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Imran Ghani
Sohaib Khalid

Home >> Articles >> LINQ >> Post New Resource Bookmark and Share   

 Subscribe to Articles

XAttribute Class: LINQ to XML Part 1

Posted By:Dhananjay Kumar       Posted Date: March 01, 2010    Points: 25    Category: LINQ    URL: http://www.dotnetspark.com  

XAttribute Class: LINQ to XML Part 1. In this article, I will give explanation on XAttribute class. This class is used to construct Attributes in XML Elements.


In this article, I will give explanation on XAttribute class. This class is used to construct Attributes in XML Elements. After completing this article, you will learn the purpose of XAttribute class and how to use it in LINQ to SQL.

What is an Attribute in XML?

XML Attribute is a Name/Value pair associated with XML elements. The XAttribute class represents an XML Attribute.

XAttribute class

XAttribute class is defined as below in namespace System.Xml.Linq. And it inherits the class XObject.

using System;
using System.Collections.Generic;
using System.Xml;
namespace System.Xml.Linq
    public class XAttribute : XObject
        public XAttribute(XAttribute other);

5 Facts:

  1. XElement contains a list of attribute for that element.
  2. Attributes must have unique name for that particular element.
  3. Attributes are not node they are Key/value pair associated with an element.
  4. Attributes are maintained in XML tree in order they are added to the element.
  5. When attributes are returned from element they are not sorted.

Constructor of XAttribute

If you see above definition of XAttribute class; there are two constructors. Usually we use the second constructor as following:

public XAttribute(XName name, object value);

name: It is unique name of Attribute in XML Element. It is of type XName.
value: It is value of the attribute. It is of type object.


In below example, I am constructing an XElment called xmltree. This root element is having many nested XElement,

  1. XElement Data1 is having one XAttribute. Name of the Attribute is "name" and value of attribute is "dj".
  2. XElement Data2 is having two XAttribute. They are ID and DEPT with values U18949 and MIT respectively.

XElement xmltree = new XElement("Root",
  new XElement("Data1", new XAttribute("name", "Dj"), 1),
  new XElement("Data2", new XAttribute("ID", "U18949"),
  new XElement("Data3", "3"),
  new XElement("Data4", "4")


The output of above code looks like following.



In this article, I explained about XAttribute class available in LINQ to SQL. In next article, I will explain about XElement class. Thanks for reading. 

 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend