Results 1 to 5 of 5

Thread: [RESOLVED] Return XML values sorted

  1. #1
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 05
    Location
    Sexbierum (Netherlands)
    Posts
    2,174

    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
    Super Moderator Joacim Andersson's Avatar
    Join Date
    Jan 99
    Location
    Sweden
    Posts
    13,401

    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
    Joacim Andersson
    Microsoft MVP, MCSD, MCSE, Sun Certified Java Programmer
    If anyone's answer has helped you, please show your appreciation by rating that answer.
    I'd rather run ScriptBrix...
    Joacim's view on stuff.

  3. #3
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 05
    Location
    Sexbierum (Netherlands)
    Posts
    2,174

    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
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 05
    Location
    Sexbierum (Netherlands)
    Posts
    2,174

    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
    Super Moderator Joacim Andersson's Avatar
    Join Date
    Jan 99
    Location
    Sweden
    Posts
    13,401

    Re: [RESOLVED] Return XML values sorted

    You can also use the axis properties in VB to access the attributes
    Code:
    Dim url As String = el.@url.Value
    Joacim Andersson
    Microsoft MVP, MCSD, MCSE, Sun Certified Java Programmer
    If anyone's answer has helped you, please show your appreciation by rating that answer.
    I'd rather run ScriptBrix...
    Joacim's view on stuff.

Posting Permissions

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