LINQ to XML : Two important classes XElement and XAttribute

XElement and XAttribute are the two very important classes available in System.Xml.Linq.dll assembly. Using these two classes you can do lot of things in the LINQ to XML world. I will show you step by step how,

 

For just an element

XElement _root = new XElement("root");

_root.Save(fileName);

 

And the output will look like,

 

<?xml version="1.0" encoding="utf-8" ?>

    <root />

 

Now if you want to add child to the root,

 

XElement _root = new XElement("root");

XElement _child = new XElement("child");

_root.Add(_child);

 

The generated XML would look like,

<?xml version="1.0" encoding="utf-8" ?>

<root>

    <child />

</root>

 

This work like DOM.

 

Now the .Add() method allows you pass values in form of object. So we can pass anything there,

 

XElement _root = new XElement("root");

XElement _child = new XElement("child");

XAttribute attr = new XAttribute("attrbt", 2008);

_root.Add(_child);

_root.Add(attr);

 

The XML view will be like,

 

<?xml version="1.0" encoding="utf-8" ?>

<root attrbt="2008">

<child />

</root>

Now another interesting part is that the Add() method not only takes an object it also accepts array of objects. So you ideally do not have to call .Add() multiple times.

 

With the same output as before you can create you code,

XElement _root = new XElement("root");

XElement _child = new XElement("child");

XAttribute attr = new XAttribute("attrbt", 2008);

_root.Add(_child, attr);

 

For more child you can simply keep on adding elements to the Add() method separated by comma.

 

XElement _root = new XElement("root");

XElement _child1 = new XElement("child1");

XElement _child2 = new XElement("child2");

XElement _child3 = new XElement("child3");

XAttribute attr = new XAttribute("attrbt", 2008);

_root.Add(_child1, _child2, _child3, attr);

 

Now the output goes,

 

<?xml version="1.0" encoding="utf-8" ?>

<root attrbt="2008">

             <child1 />

<child2 />

<child3 />

</root>

 

By using List<T> you can elegantly add elements to the root. Same output for this code,

 

XElement _root = new XElement("root");

List<XElement> childs = new List<XElement>

    {

        new XElement("child1"),

        new XElement("child2"),

        new XElement("child3")

    };

XAttribute attr = new XAttribute("attrbt", 2008);

_root.Add(childs, attr);

 

Now with all these options you can go ahead and use the constructor to add elements and attribute to your root.

 

So if I want to generate previous XML in one liner way, I could do this,

 

XElement _root = new XElement("root",

   

    new List<XElement>

    {

        new XElement("child1"),

        new XElement("child2"),

        new XElement("child3")

    },

    new XAttribute("attrbt", 2008));

//_root.Add(childs, attr);

 

Now I do not require Add() method at all.

 

Hope this gives you the idea about the basics of XElement and XAttribute. Thanks to Mike Taulty , I have learnt this from one of his finest demos.

 

Namoskar!!!