Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8381505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:42:53+00:00 2026-06-09T16:42:53+00:00

I’m still somewhat new to Visual Basic and XML so I don’t know if

  • 0

I’m still somewhat new to Visual Basic and XML so I don’t know if I’m phrasing this correctly. I have only scratched the surface of XSLT, but from what I’ve seen so far I don’t know if it can do this.

I am trying to recursively sort an XML file alphabetically by its tags in Visual Basic. For example, for an XML document, such as:

<catalog>
  <game>
      <title>Role-playing EXTREME Adventure</title>
      <platform>PC</platform>
      <type>Action/Adventure</type>
      <price>60.00</price>
  </game>
  <cd>
      <title>Music Notes</title>
      <artist>Susan Dylan</artist>
      <genre>Rock</genre>
      <company>Columbia</company>
      <year>1995</year>
      <price>10.95</price>
  </cd>
  <book>
      <title>Pictures of Things</title>
      <author>Bob Smith</author>
      <type>paper-back</type>
      <price>5.99</price>
  </book>
</catalog>

…becomes:

<catalog>
  <book>
      <author>Bob Smith</author>
      <price>5.99</price>
      <title>Pictures of Things</title>
      <type>paper-back</type>
  </book>
  <cd>
      <artist>Susan Dylan</artist>
      <company>Columbia</company>
      <genre>Rock</genre>
      <price>10.95</price>
      <title>Music Notes</title>
      <year>1995</year>
  </cd>
  <game>
      <platform>PC</platform>
      <price>60.00</price>
      <title>Role-playing EXTREME Adventure</title>
      <type>Action/Adventure</type>
  </game>
</catalog>

This sorted version should replace the contents of the XML file and be saved. This will be done for multiple, long XML files. I’m not at my work computer, so I don’t have access to the mess of Visual Basic code that I had been fiddling with. I’m beginning to think I should discard what I’ve done anyway though.

I apologize if this is answered somewhere. I spent most of the day searching and working on this. So if there is already an answer, I look forward to getting a link to it. 🙂

I saw this but it doesn’t seem to meet my needs. It must be done from within a Visual Basic and not a command line.

I appreciate any help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T16:42:54+00:00Added an answer on June 9, 2026 at 4:42 pm

    Here’s a program you can use to sort XML (you’ll need at least .NET 3.0 to use LINQ to XML and extension methods, but this can also be accomplished with the old-style XML parsing libraries and regular methods):

    Imports System.Xml.Linq
    Imports System.Runtime.CompilerServices
    
    Module Module1
    
    Sub Main()
        Dim xe As XElement = XElement.Load("test.xml")
        Console.WriteLine("----Original XML----")
        Console.WriteLine(xe)
        xe.SortRecursive()
        Console.WriteLine("----Sorted XML----")
        Console.WriteLine(xe)
    End Sub
    
    ''' <summary>
    ''' Sorts the subelements of an XML element, and their subelements recursively.
    ''' </summary>
    ''' <param name="xe"></param>
    ''' <remarks></remarks>
    <Extension()>
    Sub SortRecursive(xe As XElement)
        xe.Sort()
        For Each subelement As XElement In xe.Elements()
            subelement.SortRecursive()
        Next
    End Sub
    
    ''' <summary>
    ''' Sorts the subelements of an XML element.
    ''' </summary>
    ''' <param name="xe">The element whose subelements are to be sorted.</param>
    <Extension()>
    Sub Sort(xe As XElement)
        ' Save off the subelements into a list, and remove them from the main element
        Dim subelements As New List(Of XElement)
        For Each subelement As XElement In xe.Elements().ToArray()
            subelement.Remove()
            subelements.Add(subelement)
        Next
    
        ' Sort the list of subelements
        subelements.Sort(AddressOf CompareElementNames)
    
        ' Add the elements back and return when done
        For Each subelement As XElement In subelements
            xe.Add(subelement)
        Next
    End Sub
    
    ''' <summary>
    ''' Compares two XML elements by their names.
    ''' </summary>
    ''' <param name="e1">The first element.</param>
    ''' <param name="e2">The second element.</param>
    ''' <returns>positive/negative/zero depending on comparison (same as string comparison)</returns>
    Function CompareElementNames(e1 As XElement, e2 As XElement) As Integer
        Return String.Compare(e1.Name.ToString(), e2.Name.ToString())
    End Function
    End Module
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I know there's a lot of other questions out there that deal with this
I don't have much knowledge about the IPv6 protocol, so sorry if the question
Does anyone know how can I replace this 2 symbol below from the string
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.