| |   Skin:   
      User: Not logged in 
Newsletter Signup
XSLT Library
Latest XML Blogs
Featured Examples
Featured Articles
Book Chapters
Training Courses
Sign In
My Profile
My Articles
My Examples
My Favorites
My Resources
Add a Resource
About Me
My Blog
HeadGeek Articles
Talking Portfolio
World Trip Pics

Using XmlDataSource
Author: Ivan Osmak
Published: 6/13/2005 5:56 PM
Category: ASP.NET, XML.NET
Summary: Binding xml file to a GridView with use of XmlDataSource.




The coolest thing about XmlDataSource is that you can bind it to a control (in this example GridView) without writing any non-declarative code. And if you are using VWD 2005 Express, it takes approximately 30 seconds to do this. Nice job, MS!

1. Create XmlDataSource

Insert this tag on the web form:

<asp:XmlDataSource ID="xmlAuthors" runat="server" DataFile="~/xml/books.xml" XPath="bookstore/authors"> </asp:XmlDataSource>

"DataFile" attribute is a relative path to the xml file, and XPath attribute can be used to retrieve only specific data. In this case I have used XPath to retrieve only the child node "authors" whose parent is "bookstore".

2. Create GridView control (it's a successor of Asp.Net1 DataGrid)

<asp:GridView ID="gvwAuthors" runat="server" AutoGenerateColumns="False" DataSourceID="xmlAuthors" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4"  ForeColor="Black" GridLines="Vertical">
 <FooterStyle BackColor="#CCCC99" />
<RowStyle BackColor="#F7F7DE" />
<asp:BoundField DataField="name" HeaderText="Name" SortExpression="name" /> <asp:BoundField DataField="country" HeaderText="Country" SortExpression="country" /> <asp:ButtonField CommandName="ShowBooks" Text="Show books" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" /> <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" /> <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" /> <AlternatingRowStyle BackColor="White" />

The crucial attribute here is "DataSourceID" which specifies "xmlAuthors", the XmlDataSource we have created in first step.


And that's all there is to it. No one single line of code. No need for gvwAuthors.DataBind(), absolutely nothing! NOTE: The "Show books" link is there, but it doesn't really do anything. Keep reading examples and I'll let you into a great mystery of "Show books" link. :)




Fans of "The Office"
Dwight Bobbleheads are here!
  “It's me! I'm the bobblehead! Yes!”

Advertise on XMLPitstop

Advertise on XMLPitstop

EggHead Cafe
Web Servicee development
Buy text ads here!
secure online backup
Color Laser Printer
Marc jacobs sunglasses
Skype vs. sipcall
VoIP Internettelefonie
Buy text ads here!

Interested in Text ads?
2,223 Total Members
36 members(last 30 days)
8 members(last 7 days)
2 members(today)

1,609 Total Discussions
12 Posts(last 30 days)
0 Posts(last 7 days)
0 Posts(today)

17,260 Total Blog Posts
1,839 Blogs(last 30 days)
342 Blogs(last 7 days)
49 Blogs(today)

8,699 Newsgroup Posts
0 Posts(last 30 days)
0 Posts(last 7 days)
0 Posts(today)

13,786 Total Resources
5 Resources(last 30 days)
1 Resources(last 7 days)
0 Resources(today)


David Silverlight's| 2801 Florida Ave #225|Miami, FL 33133|Ph:305-447-1139