HI,
I'm trying to sort this list of xml data alphabetically (supplier name) but for some reason this code is not working. Coud you help? Thanks!
My xml
CONTRACTS
-CONTRACT
---SUPPLIER
----SUPPLIERID
----SUPPLIERNAME
My .net.vb page - Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
-
-
-
Dim myString As StringBuilder = New StringBuilder(100)
-
-
Dim xdoc As New XPathDocument(AppDomain.CurrentDomain.BaseDirectory + "/cupid/local_xml.xml")
-
-
-
Dim nav As XPathNavigator = xdoc.CreateNavigator()
-
-
Dim expr As XPathExpression
-
-
expr = nav.Compile("/pf:CONTRACTS/pf:CONTRACT/pf:SUPPLIERS/pf:SUPPLIER[not(pf:SUPPLIERID=preceding::pf:SUPPLIER/pf:SUPPLIERID)]")
-
-
-
Dim namespaceManager As XmlNamespaceManager = New XmlNamespaceManager(nav.NameTable)
-
namespaceManager.AddNamespace("pf", "http://namespace.com/")
-
-
expr.AddSort("/pf:CONTRACTS/pf:CONTRACT/pf:SUPPLIERS/pf:SUPPLIER/pf:SUPPLIERNAME", XmlSortOrder.Ascending, 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='300px' border='0' cellpadding='0' cellspacing='0' border='0' class='datatable1'>")
-
myString.AppendLine("<th>Supplier name</th>")
-
-
While nodes.MoveNext()
-
-
Dim sChars As String = " "
-
-
Dim supplier As XPathNavigator = nodes.Current.SelectSingleNode("pf:SUPPLIERNAME", namespaceManager)
-
Dim supplierID As XPathNavigator = nodes.Current.SelectSingleNode("pf:SUPPLIERID", namespaceManager)
-
-
-
myString.AppendLine("<tr><td><a href=""javascript:poptastic('http://www.cupid.ac.uk/Supplier/SupplierDisplay.aspx?supplierId=" & supplierID.ToString() & "');"">" & supplier.ToString.TrimEnd(sChars) & "</a>")
-
-
myString.AppendLine("</td></tr>")
-
-
End While
-
-
myString.AppendLine("</table>")
-
-
Dim strOutput As String = myString.ToString()
-
lblOutput.Text = strOutput
-
-
-
Else
-
-
lblOutput.Text = "No results for your search"
-
-
End If
-
End Sub
6 1864
What result are you getting? If you're getting nothing I would consider adding the namespace manager to the XPathExpression before you give it the xpath or the sort.
Hi,
I get the list but NOT in alphabetical order, I wonder why.
Thanks
I think this is a context problem. Try shortening the sort path to the relative path:
../pf:SUPPLIERNAME
Otherwise, each of them will all be sorting based on the first SupplierName, which will, of course, always be the same; thus no sort.
@jkmyoung
If you can, if your project is .NET => 3.0, than consider using LINQ.
You can see here an xml I wrote to test the sorting. -
<fish>
-
<finish></finish>
-
<fool>
-
<coincidence>
-
-
</coincidence>
-
</fool>
-
<end>
-
<begin>
-
-
</begin>
-
</end>
-
<wisdom>
-
<yahoo>
-
-
</yahoo>
-
<google></google>
-
</wisdom>
-
<zebra></zebra>
-
<llama></llama>
-
</fish>
-
Now the code that I wrote, -
public static void SortElements()
-
{
-
XElement xElement = XElement.Load("TestSorting.xml");
-
-
int i = 0;
-
-
xElement.DescendantsAndSelf().ToList().ForEach((XElement el) => PrintXElementName(el, i++));
-
-
Console.ReadKey();
-
Console.WriteLine();
-
-
List<XElement> sortedElements = xElement.DescendantsAndSelf().OrderBy(el => el.Name.ToString()).ToList();
-
-
i = 0;
-
-
sortedElements.ForEach((XElement el) => PrintXElementName(el,i++));
-
-
Console.ReadKey();
-
}
-
-
private static void PrintXElementName(XElement el, int index)
-
{
-
Console.WriteLine(string.Format("Element #{0}: {1}",index, el.Name));
-
}
-
And the output of these -
Element #0: fish
-
Element #1: finish
-
Element #2: fool
-
Element #3: coincidence
-
Element #4: end
-
Element #5: begin
-
Element #6: wisdom
-
Element #7: yahoo
-
Element #8: google
-
Element #9: zebra
-
Element #10: llama
-
-
Element #0: begin
-
Element #1: coincidence
-
Element #2: end
-
Element #3: finish
-
Element #4: fish
-
Element #5: fool
-
Element #6: google
-
Element #7: llama
-
Element #8: wisdom
-
Element #9: yahoo
-
Element #10: zebra
-
And that is all.
Hi
Thanks but shortening the sorting path did not help and I am not using .net 3/LINQ
Any help?
Thanks!
Is there only ever a single supplierID and supplierName per Supplier?
I think I misread the expression. The sort should be just:
pf:SUPPLIERNAME
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Matthias Kaeppler |
last post by:
Hi,
in my program, I have to sort containers of objects which can be 2000
items big in some cases. Since STL containers are based around copying
and since I need to sort these containers quite...
|
by: mike |
last post by:
If I had a date in the format "01-Jan-05" it does not sort properly
with my sort routine:
function compareDate(a,b)
{
var date_a = new Date(a);
var date_b = new Date(b);
if (date_a < date_b)...
|
by: Xah Lee |
last post by:
Sort a List
Xah Lee, 200510
In this page, we show how to sort a list in Python & Perl and also
discuss some math of sort.
To sort a list in Python, use the “sort” method. For example:
...
|
by: kan |
last post by:
I am sorry,I have poor english..
Two Class file is located below one namespace..
it is no error..
namespace Convert (namespace name)
WinForm (first class name)
ColumnSorter (second class...
|
by: Julie |
last post by:
What is the *fastest* way in .NET to search large on-disk text files (100+ MB)
for a given string.
The files are unindexed and unsorted, and for the purposes of my immediate
requirements, can't...
|
by: Dan Stromberg |
last post by:
Hi folks.
Python appears to have a good sort method, but when sorting array elements
that are very large, and hence have very expensive compares, is there some
sort of already-available sort...
|
by: Claudio Grondi |
last post by:
I have a 250 Gbyte file (occupies the whole hard drive space) and want
to change only eight bytes in this file at a given offset of appr. 200
Gbyte (all other data in that file should remain...
|
by: =?Utf-8?B?YmJkb2J1ZGR5?= |
last post by:
I have a grid view that pulls data from a dbf file. I set the Allow Sorting
to true and I put my code in the Sorting event.
The problem is that I can't get the sorting to work so I wrote some...
|
by: KevinADC |
last post by:
If you are entirely unfamiliar with using Perl to sort data, read the "Sorting Data with Perl - Part One and Two" articles before reading this article. Beginning Perl coders may find this article...
|
by: jrod11 |
last post by:
hi,
I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |