Confessions of a .NET Developer!

Basic Linq to XML operations

This post shall deal with Inserting, Deleting and Modifying XML data using Linq to XML.

Here is how the XML will look like:

<?xml version="1.0" encoding="utf-8" ?>
  <user id="149">
  <user id="13">
  <user id="45">

Add this XML to your project or create one using the XML Editor and set its Build Action to “Content” instead of Resource and also set Copy to Output Directory to “Copy if Newer”. With its build action set to content, I can modify the XML.

The code to Load the XML using the XDocument class under the namespace System.Xml.Linq

XDocument xDoc = XDocument.Load("XMLAccess.xml");
XElement rootElem = xDoc.Root;

And here is the way to Insert a new Record(User) into the XML :

            rootElem.Add(new XElement("user",
                new XAttribute("id", 90),
                new XElement("name", "Pulkit"),
                new XElement("age", 19),
                new XElement("company", "Infosys")));


Its quite simple using Linq, and a lot messier in case of the standard XmlDocument class.

The code to delete a User:

            XElement delElem = (from XElement elem in xDoc.Descendants("user")
                                where int.Parse(elem.Attribute("id").Value) == 90
                                select elem).Single<XElement>();


So the above Linq expression will return a single XElement of type user of id=90 and gets removed using a simple Remove() function.

And the code to update the XML :

            XElement updElem = (from XElement elem in xDoc.Descendants("age")
                                where int.Parse(elem.Parent.Attribute("id").Value)==45
                                select elem).Single<XElement>();


That’s it for now. Happy coding.


May 4, 2011 Posted by | LINQ, XML | Leave a comment

Difference between Elements and Descendants

This is a small post regarding the confusion that most XLINQ developers have when they start learning it, that what is the difference between Elements and Descendants?

Well basically both of them take the same arguments and also both return IEnumerable of type XElement, so what makes them different?
Lets take an Example :
here is the XML(it has a root node “users”) :



So the exercise is the return the collection of node “mname” :
First let us use Elements,

XDocument xDoc=XDocument.Load("C:\MyXml.xml");
 IEnumerable<XElement> query = From XElement elem in xDoc.Element("users").Element("user").Element("model").Elements("mname")
Select elem;

As you can see to get to that node we have to start from the top and reach mname node.
Not to forget that, the above code will return only the first user(ie. Tarun), it won’t go to the next user. I would suggest you not to use Elements.

But that’s not the case with Descendants. With Descendants, it returns all the node mname, irrespective of its parent node.
Like this :

IEnumerable<XElement> query=From XElement elem in xDoc.Descendants("mname")
Select elem;

Hope you got the difference.
Have a nice day!

March 7, 2011 Posted by | LINQ | Leave a comment