Hi
I'm trying to sort a table that contains four columns. I've filled this table with data from an xml file. The idea is to sort the table by clicking on the header of the column. I'm not sure if this is the correct approach. I cannot use xsl and would like to maitain the same code structure but my AddSort method of the XPathExpression class doesnt semm to work. Thanks for your help.
My xml is
I'm trying to sort a table that contains four columns. I've filled this table with data from an xml file. The idea is to sort the table by clicking on the header of the column. I'm not sure if this is the correct approach. I cannot use xsl and would like to maitain the same code structure but my AddSort method of the XPathExpression class doesnt semm to work. Thanks for your help.
My xml is
Code:
CONTRACTS
--CONTRACT
---SUPPLIER
---COMMODITIES
----COMMODITY
-----COMODDITYNAME
Dim letter As String
If Request.QueryString("letter") = "" Then
letter = "A"
ElseIf Request.QueryString("letter") = "All" Then
letter = ""
Else
letter = Request.QueryString("letter")
End If
Dim SortingOrder As String = Nothing
If Not Page.IsPostBack Then
SortingOrder = XmlSortOrder.Ascending
Session("getSortingOrder") = SortingOrder
End If
If Session("getSortingOrder") = XmlSortOrder.Ascending Then
SortingOrder = XmlSortOrder.Descending
End If
Dim order As String = Nothing
If Request.QueryString("sort") = "" Then
order = "pf:NAME"
ElseIf Request.QueryString("sort") = "name" Then
order = "pf:NAME"
ElseIf Request.QueryString("sort") = "commodity" Then
order = "pf:COMMODITIES/pf:COMMODITY/pf:COMMODITYNAME"
ElseIf Request.QueryString("sort") = "supplier" Then
order = "pf:SUPPLIER"
End If
Dim myString As StringBuilder = New StringBuilder(10)
Dim xdoc As New XPathDocument("local_xml.xml")
Dim nav As XPathNavigator = xdoc.CreateNavigator()
Dim expr As XPathExpression
expr = nav.Compile("/pf:CONTRACTS/pf:CONTRACT[pf:NAME[starts-with(., '" & letter & "')]]")
Dim namespaceManager As XmlNamespaceManager = New XmlNamespaceManager(nav.NameTable)
namespaceManager.AddNamespace("pf", "http://namespace.ac.uk/")
expr.AddSort(order, SortingOrder, XmlCaseOrder.None, String.Empty, XmlDataType.Text)
expr.SetContext(namespaceManager)
Dim nodes As XPathNodeIterator = nav.Select(expr)
If nodes.Count > 0 Then
Dim tr As String = Nothing
myString.AppendLine("<table width='96%' border='0' cellpadding='0' cellspacing='0' border='0' class='datatable1'>")
myString.AppendLine("<th width='35%'><a href='?letter=" & letter & "&sort=name'>Name</a></th><th width='35%'><a href='?letter=" & letter & "&sort=commodity'>Commodity</a></th><th width='20%'><a href='?letter=" & letter & "&sort=supplier'>Supplier</a></th>")
While nodes.MoveNext()
Dim node As XPathNavigator = nodes.Current.SelectSingleNode("pf:NAME", namespaceManager)
Dim supplier As XPathNavigator = nodes.Current.SelectSingleNode("pf:SUPPLIER", namespaceManager)
Dim commodity As XPathNavigator = nodes.Current.SelectSingleNode("pf:COMMODITIES/pf:COMMODITY/pf:COMMODITYNAME", namespaceManager)
Dim sChars As String = " "
myString.AppendLine("<tr>")
myString.AppendLine("<td>")
myString.AppendLine(node.ToString())
myString.AppendLine("</td>")
myString.AppendLine("<td>")
myString.AppendLine(commodity.ToString())
myString.AppendLine("</td>")
myString.AppendLine("<td>")
myString.AppendLine(supplier.ToString())
myString.AppendLine("</td>")
myString.AppendLine("</tr>")
End While
myString.AppendLine("</table>")
Dim strOutput As String = myString.ToString()
lblOutput.Text = strOutput
Else
lblOutput.Text = "No results for your search<br/>"
End If
Comment