Topic: Binding Listbox with XLINQ
Share/Save/Bookmark
Description: This article will show you how to bind a XLINQ Query to any datasource bindable control (e.g. DropDownList, CheckBoxList, RadioButtonList, ListBox) 
 

LINQ to XML uses the System.Xml.Linq assembly and must be registered in your project/website prior to using any of it's methods.  With the namespace/assembly registered on your page, you can Read, Write and Contruct XML data.
 
You can perform LINQ queries against the following types of XML
     - Remote HTTP URL's (like WeatherChannel.com's site)
     - In-memory XML
     - File-system (*.xml)
     - Web Services that return XML
 
From the beginning you'll see that working with LINQ is much easier than using the DocumentObjectModel's API.
 
For our example, we'll be using the XML results from WeatherChannel.com
 
<?xml version="1.0" encoding="ISO-8859-1" ?>
 
<search ver="2.0">
   <loc id="USGA0028" type="1">Atlanta, GA</loc>
   <loc id="USID0011" type="1">Atlanta, ID</loc>
   <loc id="USIL0057" type="1">Atlanta, IL</loc>
</search>  
 
Below is an example I have of using XML Linq to query the Weather Channels Location Search URL.  I have a textbox on my page called txtLocation to enter data, a button to perform the seach, and a Listbox that displays the content from the search.
 
protected void btnSearch_Click( object sender, EventArgs e )
{
   try
   {
      XElement WeatherChannelResults = XElement.Load( @"http://xoap.weather.com/search/search?where=" + txtLocation.Text );
      var query = from f in WeatherChannelResults.Elements( "loc" )
                  select new { Title = f.Value, id = f.Attribute( "id" ).value };
         
      if( query.Count() == 0 )
      {
         //Give a message showing no results returned
      }
      else
      {
         lstSearchResults.DataSource = query;
         lstSearchResults.DataValueField = "id";
         lstSearchResults.DataTextField = "Title";
         lstSearchResults.DataBind();
      }
     catch( System.Net.WebException )
     {
        // Catch Error
     }
} 
 
If you wanted to bind the Results from a file, you would only have to change the source location
 
protected void btnSearch_Click( object sender, EventArgs e )
{
   try
   {
      XElement WeatherChannelFile = XElement.Load( @"C:\MyFolder\MyXMLFile.xml" );
      var query = from f in WeatherChannelFile.Elements( "loc" )
                  select new { Title = f.Value, id = f.Attribute( "id" ).value };
         
      if( query.Count() == 0 )
      {
         //Give a message showing no results returned
      }
      else
      {
         lstSearchResults.DataSource = query;
         lstSearchResults.DataValueField = "id";
         lstSearchResults.DataTextField = "Title";
         lstSearchResults.DataBind();
      }
     catch( System.Net.WebException )
     {
        // Catch Error
     }
} 
 
Below are some examples of some queries that might be useful:
 
XElement WeatherChannelFile = XElement.Load( @"C:\MyFolder\MyXMLFile.xml" );
var query = from f in WeatherChannelFile.Elements( "loc" )
            where (string)f.Attribute( "type" ).Value == "1" 
            select new { Title = f.Value, id = f.Attribute( "id" ).value }; 
                     
var query = from f in WeatherChannelFile.Elements( "loc" )
            where f.Value.Contains( "IL" ) 
            select new { Title = f.Value, id = f.Attribute( "id" ).value }; 
 
var query = from f in WeatherChannelFile.Elements( "loc" )
            where f.Value.Contains( "IL" ) 
            select new
           
{ Title = f.Value.Substring(0, 20), id = f.Attribute( "id" ).value };
  
 
If you have a custom object and would like to put the results into your object, you can use the code below:
 
public class WeatherObject
{
   private string _Title;
   private string _Id;
 
   private string Title
   {
      get { return _Title;  }
      set { _Title = value; }
   }
 
   private string Id
   {
      get { return _Id   }
      set { _Id = value; }
   }
} 
 
protected void btnSearch_Click( object sender, EventArgs e )
{
   try
   {
      XElement WeatherChannelFile = XElement.Load( @"C:\MyFolder\MyXMLFile.xml" );
      IEnumerable<WeatherObject> myWeather = from f in WeatherChannelFile.Elements( "loc" )
                  select new WeatherObject { Title = f.Value, id = f.Attribute( "id" ).value };
         
      if( query.Count() == 0 )
      {
         //Give a message showing no results returned
      }
      else
      {
         lstSearchResults.DataSource = myWeather;
         lstSearchResults.DataValueField = "Id";
         lstSearchResults.DataTextField = "Title";
         lstSearchResults.DataBind();
      }
     catch( System.Net.WebException )
     {
        // Catch Error
     }
}