Sorting a xml file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • milecimm
    New Member
    • Oct 2007
    • 24

    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

    Code:
    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
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    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.

    Comment

    • milecimm
      New Member
      • Oct 2007
      • 24

      #3
      Hi,
      I get the list but NOT in alphabetical order, I wonder why.

      Thanks

      Comment

      • jkmyoung
        Recognized Expert Top Contributor
        • Mar 2006
        • 2057

        #4
        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.
        Last edited by jkmyoung; May 11 '10, 04:38 PM. Reason: oops. Parent node.

        Comment

        • Monomachus
          Recognized Expert New Member
          • Apr 2008
          • 127

          #5
          Originally posted by jkmyoung
          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.
          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.
          Code:
          <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,
          Code:
           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
          Code:
          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.

          Comment

          • milecimm
            New Member
            • Oct 2007
            • 24

            #6
            Hi

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

            Any help?

            Thanks!

            Comment

            • jkmyoung
              Recognized Expert Top Contributor
              • Mar 2006
              • 2057

              #7
              Is there only ever a single supplierID and supplierName per Supplier?

              I think I misread the expression. The sort should be just:
              pf:SUPPLIERNAME

              Comment

              Working...