LINQ to XML Part #1-XAttribute Class

No.of Views747
Bookmarked0 times
Downloads 
Votes0
By  Dhananjay Kumar   On  17 Apr 2010 10:04:19
Tag : LINQ , Miscellaneous
In this article, I will give explanation on XAttribute class. This class is used to construct Attributes in XML Elements.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Objective

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

What is Attribute in XML?


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

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);
        
        public XAttribute(XName name, object value);
        …….
}
}

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. 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.

Example


In below example; I am constructing a 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 XAttribute("DEPT","MIT"),2),
                                 new XElement("Data3", "3"),
                                 new XElement("Data4", "4")
                                );
Console.WriteLine(xmltree);            


  Sample OutPut

Image Loading

Conclusion

In this article; I explained about XAttribute class. In next article, I will explain about XElement class. Thanks for reading.

 
Sign Up to vote for this article
 
About Author
 
Dhananjay Kumar
Occupation-Software Engineer
Company-Infosys Technolgies,Pune
Member Type-Gold
Location-India
Joined date-20 Jul 2009
Home Page-http://dhananjaykumar.net/
Blog Page-http://dhananjaykumar.net/
Dhananjay Kumar is Microsoft MVP on connected system. He blogs at http://dhananjaykumar.net/ . You can follow him http://twitter.com/debugmode_/ and reach him at dhananjay.25july@gmail.com
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top