Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
|
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 9th, 2010, 10:23 AM
Registered User
 
Join Date: Apr 2010
Posts: 21
Thanks: 12
Thanked 0 Times in 0 Posts
Send a message via AIM to QuadFather
Question How to populate listbox with xml data: VB2008

I'm programming in VB, using Visual Studio 2008.
This is a Windows Forms program.

GOAL: I want to populate a multi column listbox with the <Name>, <Calories>, and <Fat> of each <Food> listed under a certain <Profile>.<Date>.<Name> from an xml file when I click Button1. I've accomplished this using Excel spreadsheets as my data source, but now I want to use xml files for flexibility and portability.

Objects I'm using:
  • ListBox1
  • Button1

Here is my xml file (Foods.xml):
Code:
<?xml version="1.0" encoding="utf-8"?>
<Profiles>
  <Profile>
    <Name>Kelly</Name>
    <Date>
      <Name>Thursday: April 05, 2010</Name>
      <Food>
        <Name>Burger</Name>
        <Calories>100</Calories>
        <Fat>10</Fat>
      </Food>
    </Date>
  </Profile>
  <Profile>
    <Name>Brandon</Name>
    <Date>
      <Name>Thursday: April 08, 2010</Name>
      <Food>
        <Name>Fries</Name>
        <Calories>100</Calories>
        <Fat>10</Fat>
      </Food>
    </Date>
  </Profile>
</Profiles>
I don't even know where to begin with ListBoxes ... most of the online help seems to be for earlier versions of VB, and VisualStudio 2008 is telling me that ".AddItem" is invalid syntax for ListBox1.

Anyone out there smarter than me?

EDIT:
Here is my latest attempt:
Code:
ListBox1.ObjectCollection.AddRange(New Object() {"System.Xml"})

Last edited by QuadFather; April 9th, 2010 at 10:27 AM.. Reason: made further attempts
 
Old April 9th, 2010, 10:32 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

What is wrong with :

ListBox1.Items.Add("Test")

I'm not sure a multi column listbox is what you think it is though. It is simply a way of showing multiple items over more than one column, not for showing multiple bits of data for a single item (as was the case in Classic VB or Microsoft Access).

You'll probably have to use ListView instead to do what you want.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
The Following User Says Thank You to samjudson For This Useful Post:
QuadFather (April 9th, 2010)
 
Old April 9th, 2010, 10:44 AM
Registered User
 
Join Date: Apr 2010
Posts: 21
Thanks: 12
Thanked 0 Times in 0 Posts
Send a message via AIM to QuadFather
Default

Ok, I'm trying with a ListView1 now, set as "details".

The code I tried (and didn't work) is:

Code:
Dim FoodsCatalogue As XDocument = XDocument.Load("Foods.xml")
ListView1.Items.Add(FoodsCatalogue)
 
Old April 9th, 2010, 01:07 PM
Registered User
 
Join Date: Apr 2010
Posts: 21
Thanks: 12
Thanked 0 Times in 0 Posts
Send a message via AIM to QuadFather
Unhappy Still not working ...

Tried the following code, to no avail:

Code:
Dim Profiles As XDocument = XDocument.Load("Profiles.xml")
        ListView1.Items.AddRange(New ListViewItem(Profiles.<Profile>.<Name>.Select(Function(n) n.Value).OrderBy(Function(s) s).ToArray()))


And this:
Code:
ListView1.Items.AddRange(New ListViewItem() {Profiles.<Profile>.<Name>.Select(Function(n) n.Value).OrderBy(Function(s) s).ToArray())})

Last edited by QuadFather; April 9th, 2010 at 01:10 PM.. Reason: another attempt
 
Old April 12th, 2010, 10:43 AM
Registered User
 
Join Date: Apr 2010
Posts: 21
Thanks: 12
Thanked 0 Times in 0 Posts
Send a message via AIM to QuadFather
Default

I'm about to give up. Can't find any documentation on this anywhere for VB.





Similar Threads
Thread Thread Starter Forum Replies Last Post
how to populate a table with xml data in xslt miccipynewbie XSLT 6 May 28th, 2007 07:29 AM
Populate ListBox seananderson Access VBA 13 April 18th, 2007 11:46 AM
How to: Populate Filename to listbox? mdtcao Beginning VB 6 1 May 11th, 2005 10:25 PM
How To Populate ListBox from a Secondary Data Sour luca ASP.NET 1.0 and 1.1 Basics 4 December 1st, 2004 05:24 PM
Listbox doesn't populate all the records edcaru Access 1 April 25th, 2004 10:32 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.