dotnetspider.com
Login Login    Register      

TutorialsForumCareer DevelopmentResourcesReviewsJobsInterviewCommunitiesProjectsTraining

Subscribe to Subscribers
Talk to Webmaster
Tony John

Facebook
Google+
Twitter
LinkedIn
Online MembersNanda Kishore
srirama
Shine S
Phagu Mahato
More...
Join our online Google+ community for Bloggers, Content Writers and Webmasters




Resources » Code Snippets » LINQ Samples

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


Posted Date:     Category: LINQ Samples    
Author: Member Level: Gold    Points: 30


Here i will be demonstrating how to query the XML which i am getting from the URL and converting the XML data in List Object. After get the Object List, i will be binding data to Gridview to see the details about the Currency and Rates.



 


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("http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml");

XElement element = XElement.Parse(xmlData);

XNamespace ns = "http://www.ecb.int/vocabulary/2002-08-01/eurofxref";
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
}).ToList();


GridView1.DataSource = lstdata;
GridView1.DataBind();
}
}
}


.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; }
}
}






Did you like this resource? Share it with your friends and show your love!


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

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

Feedbacks      

Post 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:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: Iterate two lists using linq, how to convert a for loop to linq method group
    Previous Resource: Different Sorting techniques (delegate with anonymous method, lamda)
    Return to Resources
    Post New Resource
    Category: LINQ Samples


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    (No tags found.)



    Follow us on Twitter: https://twitter.com/dotnetspider

    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Email subscription
  • .NET Jobs
  • .NET Articles
  • .NET Forums
  • Articles Rss Feeds
    Forum Rss Feeds


    About Us    Contact Us    Copyright    Privacy Policy    Terms Of Use    Revenue Sharing sites   Advertise   Talk to Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.