You must be familiar with first part of the code which retrieves dataset using DataAdapter, once we have data set we will name dataset and datatable so that xml output has proper element name instead of default "DataSet" and "Table1" as element, next part is most important for us, we will set DataRelation in that part.
We will use DataRelation constructor which takes four parameters. First parameter is name of relation, second parameter is parentColumn which in our case is "EmployeeID" column and third parameter is child Column which in our case is "ReportsTo" and last one is createConstraints we will set it to true because we know all data is valid in our table.
And then we will set Nested property to true so that dataset can create nested XML for us.
Generated XML
We will use DataSet.GetXml() method to generate XML out of dataset, following is the structure of XML generated by dataset. As you can see employeeid 1 is child of employeeid 2.
<Employees>
<Employee>
<EmployeeID>2</EmployeeID>
<FirstName>Andrew</FirstName>
<Title>Vice President, Sales</Title>
<Employee>
<EmployeeID>1</EmployeeID>
<FirstName>Nancy</FirstName>
<Title>Sales Representative</Title>
<ReportsTo>2</ReportsTo>
</Employee>
...
</Employee>
</Employees>
Transforming XML using XSLT
XSL Transformation is one of the best option if you want to display nested data, actually before Sitemap and TreeControl which came with ASP.Net 2.0 XSL transformation was the only neat way to display hierarchy.
We will use System.Web.UI.WebControl.Xml control to display xml content using XSL Transformation. Xml control requires XmlDocument and Transformsource to transform the document.
Following is the XSLT that we will apply to XML document. XSLT will perform recursive transformation to generate nested list.
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" />
<xsl:template match="/Employees">
<xsl:call-template name="EmployeeListing" />
</xsl:template>
<xsl:template name="EmployeeListing">
<ul>
<xsl:apply-templates select="Employee" />
</ul>
</xsl:template>
<xsl:template match="Employee">
<li>
<xsl:value-of select="FirstName"/>
<xsl:if test="count(Employee) > 0">
<xsl:call-template name="EmployeeListing" />
</xsl:if>
</li>
</xsl:template>
</xsl:stylesheet>
Following is the final result.

End notes
This sample displays nested list, but you can use same principle to display tree structure, site menu and many other hierarchal data. A part that I have not touched in this sample is retrieving data only under certain node, but you will be able to find methods of doing that by searching on internet. Also take a look Joe Celko's favorite Nested Set model for storing hierarchy in SQL.
Run sample code
NOTE: THIS ARTICLE IS CONVERTED FROM C# TO VB.NET USING A CONVERSION TOOL. ORIGINAL ARTICLE CAN BE FOUND ON C# CORNER (WWW.C-SHARPCORNER.COM).