LINQ To XML
Language Integrated Query (LINQ, pronounced “link“) is a Microsoft .NET Framework component that adds native data querying capabilities to .NET languages.(#)
LINQ to XML is a built-in LINQ data provider that is implemented within the “System.Xml.Linq” namespace in .NET 3.5.
As Microsoft says, LINQ to XML provides a clean programming model that enables you to read, construct and write XML data. LINQ to XML can be used to perform LINQ queries over XML to retrieve from your local server or from a remote HTTP URL or web-service, or from any in-memory XML content.
They says that LINQ to XML provides much richer (and easier) querying and data shaping support than the low-level XmlReader/XmlWriter API in .NET. It also ends up being much more efficient (and uses much less memory) than the DOM API that XmlDocument provides.
Today I will give a simple example that uses LINQ to XML (VB.NET and C#)
First we create new ASP.NET 3.5 website using Visual Studio 2008 or any IDE that supports ASP.NET 3.5.
Then we will create XML file, we assumed that the file contains info about websites, lets say, website name and website link (url), website node contains lang attribute, which contains the language of the site, also we can add it as a node if we want, the XML file appears as follows:
<?xml version=“1.0“ encoding=“utf-8“ ?>
<Websites>
<Website lang=“en“>
<Name>Ibrahim Dwaikat</Name>
<Url>http://www.idwaikat.me</Url>
</Website>
<Website lang=“en“>
<Name>Yahoo</Name>
<Url>http://www.Yahoo.com</Url>
</Website>
<Website lang=“ar“>
<Name>Balata-Albalad</Name>
<Url>http://www.balata-albalad.org</Url>
</Website>
<Website lang=“ar“>
<Name>Google Jordan</Name>
<Url>http://www.google.jo</Url>
</Website>
</Websites>
I think the XML clearify itself … no need for more details about its schema.
Now we save the XML in the root folder of our website, named : XMLFile.xml
In Page_Load we write the following code (VB.NET) – after a moments I will write C# code too, very similar,
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim xmlDoc As XDocument = XDocument.Load(Server.MapPath(“XMLFile.xml”))
Dim xmlData = From webSite In xmlDoc.Descendants(“Website”) _
Where (webSite.Attribute(“lang”).Value = “ar”) _
Select Name = webSite.Element(“Name”).Value, _
Url = webSite.Element(“Url”).Value
For Each item In xmlData
Response.Write(“<a href='” & item.Url & “‘>” & item.Name & “</a> <br />”)
Next
End Sub
Now I will talk about the code line by line.
Dim xmlDoc As XDocument = XDocument.Load(Server.MapPath(“XMLFile.xml”))
Here I created an instance form XDocument (which belongs to System.XML.Linq namespace), and using the method load, I loaded the previous XML file which named XMLFile.XML, here I used Server.MapPath helper method which returns the correct path of the XML file, because sometimes when we didn’t use it, the relative path can be not as we expected.
The next step is making a query that returns Website Name and URL, the condition is that the language is Arabic (ar).
Dim xmlData = From webSite In xmlDoc.Descendants(“Website”) _
we define a variable (generic collection, typeless here) that will contain the query result (xmlData), and we start the Linq by the keyword: From, followed by XElement which is here website, followed by In xDocument.Descendants, which takesSystem.XML.Linq.XName, and returns a filtered collection of the descendant elements for this document or element in document order, only elements that have a matching System.XML.Linq.XName are included in the returned collection.
Where (webSite.Attribute(“lang”).Value = “ar”) _
Where represents the condition, linq condition supports many comparisons like =, <>, is, IsNot … etc.
Select Name = webSite.Element(“Name”).Value, _
Url = webSite.Element(“Url”).Value
Here we specify what the query will return, we will return the Name, and the URL, if you know the basics of DOM API in .NET its easier to understand the meaning of this.
For Each item In xmlData
now we make a For Each loop to get the records in the xmlData collection.
Response.Write(“<a href='” & item.Url & “‘>” & item.Name & “</a> <br />”)
Now we use Response.Write to write the results in the page, as a conventional href : <a href=’URL’>TEXT</a> and we put new line..
Finally we close the for each loop using Next, and when we run the application we will get:
Here is the same code in C#
protected void Page_Load(object sender, EventArgs e)
{
XDocument xmlDoc = XDocument.Load(Server.MapPath(“XMLFile.xml”));
var xmlData = from webSite in xmlDoc.Descendants(“Website”)
where (webSite.Attribute(“lang”).Value == “ar”)
select new
{
Name = webSite.Element(“Name”).Value,
Url = webSite.Element(“Url”).Value
} ;
foreach (var item in xmlData)
{
Response.Write(“<a href='” + item.Url + “‘>” + item.Name + “</a> <br />”);
}
}
That was a very simple example, for more information you can visit the following links or you can send me a comment about your question..
Thanks for reading and applying. 🙂