Linq to XML

Generally I don’t like to do XML programming due to all the XPath and etc….

But now due to LINQ to XML , it is very easy and interesting…

 

Suppose you have following XML. Now we can create xml using XElement (System.Xml.Linq;)object. Following is the code to create the XML..

This is the simplest from just for understanding ..

Code Snippet
  1. XElement con = new XElement(“Contracts”,
  2.                                     new XElement(“Contact”, new XAttribute(“IsPermmenet”, “True”),
  3.                                         new XElement(“Office”, new XElement(“Add1″, “Simanhar”), new XElement(“City”, “Ahd”)),new XElement(“Home”,new XElement(“Add1″,“ssss”),new XElement(“City”,“AHD”))),
  4.                                     new XElement(“Contact”, new XAttribute(“IsPermmenet”, “false”),
  5.                                         new XElement(“Office”, new XElement(“Add1″, “JayNagar”), new XElement(“City”, “BNG”)),new XElement(“Home”,new XElement(“Add1″,“pppp”),new XElement(“City”,“BNG”))));

 

<Contracts>
  <Contact IsPermanent="True">
    <Office>
      <Add1>Simanhar</Add1>
      <City>Ahd</City>
    </Office>
    <Home>
      <Add1>ssss</Add1>
      <City>AHD</City>
    </Home>
  </Contact>
  <Contact IsPermanent="false">
    <Office>
      <Add1>JayNagar</Add1>
      <City>BNG</City>
    </Office>
    <Home>
      <Add1>pppp</Add1>
      <City>BNG</City>
    </Home>
  </Contact>
</Contracts>

Now we can write the LINQ for above xml.

Code Snippet
  1. XElement con = new XElement(“Contracts”,
  2.                                     new XElement(“Contact”, new XAttribute(“IsPermmenet”, “True”),
  3.                                         new XElement(“Office”, new XElement(“Add1″, “Simanhar”), new XElement(“City”, “Ahd”)),new XElement(“Home”,new XElement(“Add1″,“ssss”),new XElement(“City”,“AHD”))),
  4.                                     new XElement(“Contact”, new XAttribute(“IsPermmenet”, “false”),
  5.                                         new XElement(“Office”, new XElement(“Add1″, “JayNagar”), new XElement(“City”, “BNG”)),new XElement(“Home”,new XElement(“Add1″,“pppp”),new XElement(“City”,“BNG”))));
  6.             Console.WriteLine(con.ToString());
  7.             var cons = con.DescendantsAndSelf(“Contact”);
  8.             var con_one = from c in cons
  9.                           where c.Element(“Office”).Element(“City”).Value == “Ahd”
  10.                           let address1 = c.Element(“Office”).Element(“Add1″)
  11.                           let city = c.Element(“Office”).Element(“City”)
  12.                           select new
  13.                           {
  14.                               AddressForOffice = address1.Value.ToUpper(),
  15.                               CityForOffice = city.Value.ToUpper()
  16.                               
  17.                           };
  18.             Console.WriteLine(con_one.ToString());

If you observed above code, after creating the xml, I have used DescendentaAndSelf method of XElement to create the List of Contact. LINQ will work with collection only so I have to convert XElement into List<XElement>.

 

I have used ‘let’ keyword in between the query. ‘let’ keyword will create the local variable and store the information for the query.

If you observed, I have taken the variable address1 and city and store the value of element which office’s city is “Ahd”.

I have used both the variable when query is creating the output using select new command.

select new
                          {
                              AddressForOffice = address1.Value.ToUpper(),
                              CityForOffice = city.Value.ToUpper()
                          };

So this way we can store the value in between of linq query and after that we can apply some function (here ToUPPER()) and create the new object.

Advertisement

About miteshisheth
Believe in sharing knowledge.

2 Responses to Linq to XML

  1. Joan says:

    Can u elaborate more?

    This is good for beginners.

  2. Vimal says:

    Check this out…its nice one

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.