If for your project hierarchical nature of XML data is not important you can ignore it. In this case, you can bind the XML data source directly to an ordinary grid control such as the Grid View, by following the next steps:

1. You should define the XML data source and point it to the file that has the content you want to use:

 

<asp:XmlDataSource ID=”sourceBooks” runat=”server”

DataFile=”BooksList.xml” />

 

2. You can bind the GridView with automatically generated columns, in the same way you bind it to any other data source:

 

<asp:GridView ID=”GridView1″ runat=”server” AutoGenerateColumns=”True”

DataSourceID=” sourceBooks” />

 

When you run the page, the XmlDataSource will extract the data from the BooksList.xml file, provide it to the GridView as an XmlDocument object, and call DataBind(). Because the XmlDocument implements the IEnumerable interface, the GridView can walk through its structure in much the same way as it walks through a DataView. It traverses the XmlDocument.Nodes collection and gets all the attributes for each XmlNode.

Note: The XmlDocument.Nodes collection contains only the first level of nodes, but each of these nodes can contain nested nodes through its own XmlNode.Nodes collection. However, the IEnumerable implementation doesn’t take this into account. It walks over only the upper level of XmlNode objects, and your result will contain only the top level of nodes.

 

XML nonhierarchical data binding

XML nonhierarchical data binding

You can make this binding explicit by defining columns for each attribute:

<asp:GridView ID=”GridView1″ runat=”server” DataSourceID=”sourceBooks” AutoGenerateColumns=”False”>

<Columns>

<asp:BoundField DataField=”ISBN-13″ HeaderText=”ISBN-13″ SortExpression=”ISBN-13″ />

<asp:BoundField DataField=”Title” HeaderText=”Title” SortExpression=”Title” />

</Columns>

</asp:GridView>

 

In other words, by using this approach, you can bind only to the top- level of nodes, and you can display text only from the attributes of that node.  If you want to display other information from deeper down in XML document you should implement other techniques.