How to Query XML Data having name Space and Return List Object?

Do you want to know how to query the XML which is getting from the URL and converting the XML data in List Object? After get the Object List, want to bind the data to Gridview to see the details.

Here I will be demonstrating how to query the XML which I am getting from the REST service and converting the XML data into List Object.

After getting the Object List, I will be binding list data to Gridview to see the details about the Currency and Rates.

In below code I will be using following URL which Returns the XML having information about the Currency and Rates for European Bank.

Sample XML

Please look into the XML Structure to understand it more closely as we will be queering XML to just getting all the Currency and there Rates.

Here I am using LINQ to get all the Cube Elements which holds the Currency and Rates attributes.

Look into the Page Load method where I have to attach the name space because XML has the namespace define to its root element.

.aspx.cs code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Xml.Linq;

namespace ECBBank
public partial class _Default : System.Web.UI.Page
private string xmlData;
protected void Page_Load(object sender, EventArgs e)
WebClient client = new WebClient();
xmlData = client.DownloadString("");

XElement element = XElement.Parse(xmlData);

XNamespace ns = "";
IEnumerable data = from c in element.Elements(ns + "Cube").Elements(ns + "Cube").Elements(ns + "Cube") select c;

List lstdata = (from c in element.Elements(ns + "Cube").Elements(ns + "Cube").Elements(ns + "Cube")
select new CubeData()
Currency = c.Attribute("currency").Value,
Rate = c.Attribute("rate").Value

GridView1.DataSource = lstdata;

.aspx page

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="ECBBank._Default" %>

< asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">

< asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
< center>
< h2>
Welcome to ECB Rate Chart
< /h2>

< br />
< asp:GridView ID="GridView1" runat="server" BackColor="White"
BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" CellPadding="3"
GridLines="Horizontal" AutoGenerateColumns="false" AllowSorting="false">
< AlternatingRowStyle BackColor="#F7F7F7" />
< FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
< HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
< PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
< RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
< SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
< SortedAscendingCellStyle BackColor="#F4F4FD" />
< SortedAscendingHeaderStyle BackColor="#5A4C9D" />
< SortedDescendingCellStyle BackColor="#D8D8F0" />
< SortedDescendingHeaderStyle BackColor="#3E3277" />
< Columns>
< asp:TemplateField>
< HeaderTemplate>
< asp:Literal ID="header" Text="ID" runat="server">
< /HeaderTemplate>
< ItemTemplate>
< asp:Literal ID="id" Text='<%#Container.DataItemIndex + 1%>' runat="server">
< /ItemTemplate>
< /asp:TemplateField>

< asp:BoundField DataField="Currency" HeaderText="Currency" SortExpression="Currency"/>
< asp:BoundField DataField="Rate" HeaderText="Rate" />
< /Columns>
< /asp:GridView>

< /asp:Content>

.cs file for storing Currency and Rates

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ECBBank
public class CubeData
private string fileLocaion = "hello";
public int ID { get; set; }
public string Currency { get; set; }
public string Rate { get; set; }


No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: