473,399 Members | 4,192 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Sorting a xml file

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

Expand|Select|Wrap|Line Numbers
  1. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  2.  
  3.  
  4.         Dim myString As StringBuilder = New StringBuilder(100)
  5.  
  6.         Dim xdoc As New XPathDocument(AppDomain.CurrentDomain.BaseDirectory + "/cupid/local_xml.xml")
  7.  
  8.  
  9.         Dim nav As XPathNavigator = xdoc.CreateNavigator()
  10.  
  11.         Dim expr As XPathExpression
  12.  
  13.         expr = nav.Compile("/pf:CONTRACTS/pf:CONTRACT/pf:SUPPLIERS/pf:SUPPLIER[not(pf:SUPPLIERID=preceding::pf:SUPPLIER/pf:SUPPLIERID)]")
  14.  
  15.  
  16.         Dim namespaceManager As XmlNamespaceManager = New XmlNamespaceManager(nav.NameTable)
  17.         namespaceManager.AddNamespace("pf", "http://namespace.com/")
  18.  
  19.         expr.AddSort("/pf:CONTRACTS/pf:CONTRACT/pf:SUPPLIERS/pf:SUPPLIER/pf:SUPPLIERNAME", XmlSortOrder.Ascending, XmlCaseOrder.None, String.Empty, XmlDataType.Text)
  20.  
  21.         expr.SetContext(namespaceManager)
  22.  
  23.         Dim nodes As XPathNodeIterator = nav.Select(expr)
  24.  
  25.  
  26.         If nodes.Count > 0 Then
  27.  
  28.             Dim tr As String = Nothing
  29.  
  30.  
  31.             myString.AppendLine("<table width='300px' border='0' cellpadding='0' cellspacing='0' border='0' class='datatable1'>")
  32.             myString.AppendLine("<th>Supplier name</th>")
  33.  
  34.             While nodes.MoveNext()
  35.  
  36.                 Dim sChars As String = " "
  37.  
  38.                 Dim supplier As XPathNavigator = nodes.Current.SelectSingleNode("pf:SUPPLIERNAME", namespaceManager)
  39.                 Dim supplierID As XPathNavigator = nodes.Current.SelectSingleNode("pf:SUPPLIERID", namespaceManager)
  40.  
  41.  
  42.                 myString.AppendLine("<tr><td><a href=""javascript:poptastic('http://www.cupid.ac.uk/Supplier/SupplierDisplay.aspx?supplierId=" & supplierID.ToString() & "');"">" & supplier.ToString.TrimEnd(sChars) & "</a>")
  43.  
  44.                 myString.AppendLine("</td></tr>")
  45.  
  46.             End While
  47.  
  48.             myString.AppendLine("</table>")
  49.  
  50.             Dim strOutput As String = myString.ToString()
  51.             lblOutput.Text = strOutput
  52.  
  53.  
  54.         Else
  55.  
  56.             lblOutput.Text = "No results for your search"
  57.  
  58.         End If
  59.     End Sub
May 11 '10 #1
6 1864
jkmyoung
2,057 Expert 2GB
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.
May 11 '10 #2
Hi,
I get the list but NOT in alphabetical order, I wonder why.

Thanks
May 11 '10 #3
jkmyoung
2,057 Expert 2GB
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.
May 11 '10 #4
Monomachus
127 Expert 100+
@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.
Expand|Select|Wrap|Line Numbers
  1. <fish>
  2.   <finish></finish>
  3.   <fool>
  4.     <coincidence>
  5.  
  6.     </coincidence>
  7.   </fool>
  8.   <end>
  9.     <begin>
  10.  
  11.     </begin>
  12.   </end>
  13.   <wisdom>
  14.     <yahoo>
  15.  
  16.     </yahoo>
  17.     <google></google>
  18.   </wisdom>
  19.   <zebra></zebra>
  20.   <llama></llama>
  21. </fish>
  22.  
Now the code that I wrote,
Expand|Select|Wrap|Line Numbers
  1.  public static void SortElements()
  2.         {
  3.             XElement xElement = XElement.Load("TestSorting.xml");
  4.  
  5.             int i = 0;
  6.  
  7.             xElement.DescendantsAndSelf().ToList().ForEach((XElement el) => PrintXElementName(el, i++));
  8.  
  9.             Console.ReadKey();
  10.             Console.WriteLine();
  11.  
  12.             List<XElement> sortedElements = xElement.DescendantsAndSelf().OrderBy(el => el.Name.ToString()).ToList();
  13.  
  14.             i = 0;
  15.  
  16.             sortedElements.ForEach((XElement el) => PrintXElementName(el,i++));
  17.  
  18.             Console.ReadKey();
  19.         }
  20.  
  21.         private static void PrintXElementName(XElement el, int index)
  22.         {
  23.             Console.WriteLine(string.Format("Element #{0}: {1}",index, el.Name));
  24.         }
  25.  
And the output of these
Expand|Select|Wrap|Line Numbers
  1. Element #0: fish
  2. Element #1: finish
  3. Element #2: fool
  4. Element #3: coincidence
  5. Element #4: end
  6. Element #5: begin
  7. Element #6: wisdom
  8. Element #7: yahoo
  9. Element #8: google
  10. Element #9: zebra
  11. Element #10: llama
  12.  
  13. Element #0: begin
  14. Element #1: coincidence
  15. Element #2: end
  16. Element #3: finish
  17. Element #4: fish
  18. Element #5: fool
  19. Element #6: google
  20. Element #7: llama
  21. Element #8: wisdom
  22. Element #9: yahoo
  23. Element #10: zebra
  24.  
And that is all.
May 12 '10 #5
Hi

Thanks but shortening the sorting path did not help and I am not using .net 3/LINQ

Any help?

Thanks!
May 12 '10 #6
jkmyoung
2,057 Expert 2GB
Is there only ever a single supplierID and supplierName per Supplier?

I think I misread the expression. The sort should be just:
pf:SUPPLIERNAME
May 12 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

18
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...
22
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)...
20
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: ...
0
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...
60
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...
25
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...
16
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...
1
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...
3
KevinADC
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...
5
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
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,...
0
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...
0
Oralloy
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,...
0
jinu1996
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...
0
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...
0
tracyyun
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...
0
agi2029
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,...
0
isladogs
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.