Results 1 to 5 of 5

Thread: [RESOLVED] Return XML values sorted

  1. #1

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    Resolved [RESOLVED] Return XML values sorted

    Hi,

    I havew a smal xml file which looks like this:

    Code:
    <?xml version="1.0" encoding="utf-8" ?>
    <fv>
      <flv url="http://www.mysite1.com">Windows</flv>
      <flv url="http://www.mysite2.com">Happy</flv>
    </fv>
    I'm creating buttons in my app from the "innertext":
    vb.net Code:
    1. Dim myxml As XmlDocument = New XmlDocument()
    2.         myxml.Load(favoPath)
    3.         For Each el As XmlElement In myxml.DocumentElement.ChildNodes
    4.             Dim itm As New ButtonItem(el.InnerText, el.InnerText)
    5.             itm.Tag = el.GetAttribute("url")
    6.             'etc...
    This all works fine, all the buttons are added according the xml. I would like to know how to sort the "innertext" so the buttons are alphabetically added.
    [Happy]
    [Windows]

    Thanks in advance.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Return XML values sorted

    If you're using VS2008 or later then use XDocument and XElement instead of the older XmlDocument and XmlElement classes and use Linq to get a sorted list.
    Code:
            'Dim myxml As XDocument = XDocument.Load(favoPath)
            Dim myxml As XDocument = <?xml version="1.0" encoding="utf-8"?>
                                     <fv>
                                         <flv url="http://www.mysite8.com">Windows</flv>
                                         <flv url="http://www.mysite3.com">Happy</flv>
                                         <flv url="http://www.mysite1.com">Sorry</flv>
                                         <flv url="http://www.mysite2.com">Killer</flv>
                                     </fv>
            Dim elements = myxml.Root...<flv>.OrderBy(Function(el) el.Value)
            For Each el In elements
                'Create your buttons here
            Next

  3. #3

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    Re: Return XML values sorted

    That does work. Thank you for that. Unfortunately I have to rewrite a lot of code, cause I'm also adding and deleting. Will let you know.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  4. #4

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    Re: Return XML values sorted

    +REP. Thanks.

    There is a small issue I have left: When I get the url attribute:
    mgsbox(el.attribute("url").tostring)
    it returns exactly:
    url="www.thewebsite.com"

    I only want the www.thewebsite.com. Any suggestions?

    EDIT: Foolish me!!! It should be "("url").Value"
    Last edited by Radjesh Klauke; May 28th, 2012 at 07:09 AM.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  5. #5
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: [RESOLVED] Return XML values sorted

    You can also use the axis properties in VB to access the attributes
    Code:
    Dim url As String = [email protected]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width