Linq to XML
January 22, 2010 2 Comments
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 ..
- XElement con = new XElement(“Contracts”,
- new XElement(“Contact”, new XAttribute(“IsPermmenet”, “True”),
- new XElement(“Office”, new XElement(“Add1″, “Simanhar”), new XElement(“City”, “Ahd”)),new XElement(“Home”,new XElement(“Add1″,“ssss”),new XElement(“City”,“AHD”))),
- new XElement(“Contact”, new XAttribute(“IsPermmenet”, “false”),
- 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.
- XElement con = new XElement(“Contracts”,
- new XElement(“Contact”, new XAttribute(“IsPermmenet”, “True”),
- new XElement(“Office”, new XElement(“Add1″, “Simanhar”), new XElement(“City”, “Ahd”)),new XElement(“Home”,new XElement(“Add1″,“ssss”),new XElement(“City”,“AHD”))),
- new XElement(“Contact”, new XAttribute(“IsPermmenet”, “false”),
- new XElement(“Office”, new XElement(“Add1″, “JayNagar”), new XElement(“City”, “BNG”)),new XElement(“Home”,new XElement(“Add1″,“pppp”),new XElement(“City”,“BNG”))));
- Console.WriteLine(con.ToString());
- var cons = con.DescendantsAndSelf(“Contact”);
- var con_one = from c in cons
- where c.Element(“Office”).Element(“City”).Value == “Ahd”
- let address1 = c.Element(“Office”).Element(“Add1″)
- let city = c.Element(“Office”).Element(“City”)
- select new
- {
- AddressForOffice = address1.Value.ToUpper(),
- CityForOffice = city.Value.ToUpper()
- };
- 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.



Can u elaborate more?
This is good for beginners.
Check this out…its nice one