Show XML attribute in lable based on combobox selection...

edwardm

New member
Joined
May 12, 2006
Messages
3
Programming Experience
Beginner
[FONT=&quot]Hi all,

I'm new to vb.net 2005 and would like some help on some issues regarding populating a lable with an attribute in the same node as the one selected in a combobox. I have downloaded an xml file from msdn with all the product keys I have so far, I can populate the combobox using this code;[/FONT]
[FONT=&quot]Dim dom As New Xml.XmlDocument
dom.Load("ProductKeys.xml")
cmbProduct.Items.Clear()
Dim myAttr As Xml.XmlAttribute
For Each myAttr In dom.SelectNodes("/Your_Product_Keys/Product_Key/@Name")
cmbProduct.Items.Add(myAttr.InnerText)
Next
cmbProduct.SelectedIndex = 0[/FONT]
[FONT=&quot][/FONT]
[FONT=&quot]And the ProductKeys.xml look like this (a snippet of it anyway);[/FONT]
[FONT=&quot]<?xml version="1.0" standalone="yes" ?>
<Your_Product_Keys> [/FONT]
[FONT=&quot] <Product_Key Name="BackOffice Server 2000" Key="XXXXX-XXXXX-XXXXX-XXXXX-XXXXX" Key_Type="Retail" /> [/FONT]
[FONT=&quot] <Product_Key Name="BizTalk Server 2000" Key="XXXXX-XXXXX-XXXXX-XXXXX-XXXXX" Key_Type="Retail" /> [/FONT]
[FONT=&quot] <Product_Key Name="BizTalk Server 2000" Key="XXXXX-XXXXX-XXXXX-XXXXX-XXXXX" Key_Type="Retail" />[/FONT]
[FONT=&quot]</Your_Product_Keys>[/FONT]
[FONT=&quot] [/FONT]
[FONT=&quot]So the attribute "Name" loads fine in the combobox, but I can’t wrap my brain around a simple (or complex) way to populate the lblKey.Text property with the "Key" attribute based on the selection made in the combobox. Any pointers you can give me on how to do this?[/FONT]
 
where "dom" is the same loaded xml document (you can have "dom" loaded in global variable instead of loading it every time you select in combobox), you can do this:
VB.NET:
sub cmbProduct_selectedindexchanged(sender,e) _
handles cmbProduct.selectedindexchanged
  dim product as string = cmbProduct.items(cmbProduct.selectedindex)
  lblKey.Text = dom.selectsinglenode("/Your_Product_Keys/Product_Key[@Name=" & product & "]/@key").innertext 
end sub
This is a regular XPath query getting the @key attribute of a Product_Key node where the @Name attribute is the text from selected combobox item. I'm not at a VS right now where I can proof test this, but hope there is not a typo.
 
That is exelent JohnH! there was one syntax wrong (selectedindexchanged should be SelectedIndexChanged1 according to VS2005). I made a global var, but when I debug I get this "Expression must evaluate to a node-set". When I remove the brakets I get this "/Your_Product_Keys/Product_Key/@Name=Access 2003 Developer Extensions/@key' has an invalid token" Any Idee what can be done to fix this?
 
Ok, there was only a little typo there, your key attribute is "Key" not "key", Xml is case-sensitive, so specify the correct attribute in the XPath query and you're almost there. Now enclose the string in single qoutes else each part of the string is tried evaluated as a token. Otherwise the code is right on. The combobox event is SelectedIndexChanged. There is no such event as SelectedIndexChanged1. Well here the corrected:
VB.NET:
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] cmbProduct_SelectedIndexChanged([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) _
[/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] cmbProduct.SelectedIndexChanged
[/SIZE][SIZE=2][COLOR=#0000ff]  Dim[/COLOR][/SIZE][SIZE=2] product [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = cmbProduct.Items(cmbProduct.SelectedIndex)
  lblKey.Text = dom.SelectSingleNode([/SIZE][SIZE=2][COLOR=#800000]"/Your_Product_Keys/Product_Key[@Name='"[/COLOR][/SIZE][SIZE=2] & product & [/SIZE][SIZE=2][COLOR=#800000]"']/@Key"[/COLOR][/SIZE][SIZE=2]).InnerText
[/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]

 
Moreover, I noticed two of your selected keys are identical, which will only get you the first hit if you search by this string value. If you fill the combobox in the same order and don't change it in relation to the Xml document, then here is a variant you can use to get the node by index. The XPath function 'position()' returns a node from a 1-based node collection, like this:
VB.NET:
[SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] cmbProduct_SelectedIndexChanged([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) _
[/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] cmbProduct.SelectedIndexChanged
[/SIZE][SIZE=2] lblKey.Text = dom.SelectSingleNode([/SIZE][SIZE=2][COLOR=#800000]"/Your_Product_Keys/Product_Key[position()='"[/COLOR][/SIZE][SIZE=2] & cmbProduct.SelectedIndex + 1 & [/SIZE][SIZE=2][COLOR=#800000]"']/@Key"[/COLOR][/SIZE][SIZE=2]).InnerText
[/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]

 
Thanks JohnH! I checked the identical keys, and they are the same entry in the xml file - bug from the msdn2xml function I guess(?) Anyways, your code provided the solution - thanks for your help!
 
Back
Top