How to query XML using LINQ and DataBind
This article is all about reading xml and connecting to data source using LINQ queries. Using LINQ we can writes queries involving less code. In this article for converting raw data to xml files I have used XSL. This article also proves XSL code which help querying huge amount of raw data and convert to XML tags.
XML LINQ Data Binding
In this article I am going to explain how we can use LINQ for querying XML and bind to data sources.
Today morning I got stucked with a problem using xml as data source ..
Problem was pretty simple I have given a list of countries to populate as an xml file
<option value="74" >United Kingdom</option>
<option value="8031" >Afghanistan</option>
<option value="8032" >Albania</option>
......................................
......................................
......................................
......................................
......................................
It's huge list I need to take values from the above list and convert to XML format like this,
<countries>
<country>
<id>74</id>
<name>United States</name>
</country>
..........
..........
..........
..........
..........
..........
..........
</countries>
taking each values from the list and creating an xml manually was a tedious job so I was thinking for an alternative solution for that which can convert the above list to XML.
It took be 10 minutes to write/test an xsl application which converted the above huge list to xml format
Here is the xsl code
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:for-each select="root/option">
<xsl:text disable-output-escaping="yes"><![CDATA[<country>]]></xsl:text>
<xsl:text disable-output-escaping="yes"><![CDATA[<id>]]></xsl:text>
<xsl:value-of select="@value"/>
<xsl:text disable-output-escaping="yes"><![CDATA[</id>]]></xsl:text>
<xsl:text disable-output-escaping="yes"><![CDATA[<name>]]></xsl:text>
<xsl:value-of select="."/>
<xsl:text disable-output-escaping="yes"><![CDATA[</name>]]></xsl:text>
<xsl:text disable-output-escaping="yes"><![CDATA[</country>]]></xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Now I have generated the XML file from the raw data list.
Now I will be reading the XML list and connect to data source.
different ways are available for reading like loading to dataset and connecting to data source,since dataset is a huge object I decided to go with LINQ..
List<ListItem> item = new List<ListItem>();
XDocument xmlDoc= XDocument.Load(HttpContext.Current.Server.MapPath(XmlUrl));
var query = from q in xmlDoc.Descendants("country")
select new
{
Country = q.Element("name").Value,
Id = q.Element("id").Value
};
item.Add(new ListItem("Please Select One", "0"));
foreach (var r in query)
{
item.Add(new ListItem(r.Country, r.Id));
}
ctrlName.Items.Clear();
ctrlName.DataTextField = "Text";
ctrlName.DataValueField = "Value";
ctrlName.DataSource = item;
ctrlName.DataBind();
if you are using URL for xml you can use like
XDocument xmlDoc= XDocument.Load(Server.UrlPathEncode("http://localhost/linqLoader/Countries.xml"));
I will be loading more articles on in and out of LINQ very soon
Happy Coding
jeebu