Selecting a single node in 'XML'

J

John Barleycorn

Hi

I'm sorry if this question is a bit basic for most people, but I'm really
new to VB (I'm using the .NET 2005 Express Edition), but I've set myself a
task and would love to see it through.

The problem is that an application we use has a config file which looks
XML-like and contains the following:

<design filerevision="1" fileversion="0">
<types>
<type name="Password" default="" inherits="Password" regex=""
regexsample="">
<values />
</type>
<type name="Text" default="" inherits="Text" regex="" regexsample="">
<values />
</type>
<type name="DN" default="" inherits="DN" regex="" regexsample="">
<values />
</type>
<type name="DNs" default="" inherits="DNs" regex="" regexsample="">
<values />
</type>
<type name="Check" default="" inherits="Check" regex="" regexsample="">
<values />
</type>
<type name="lstCountries" default="" inherits="combo" regex=""
regexsample="">
<values>
<value key="US">
</value>
<value key="AD">
</value>
<value key="AE">
</value>
<value key="AF">
</value>
<value key="AG">
</value>
<value key="AI">
</value>
<value key="AL">
</value>
<value key="AM">
</value>
</values>
</type>
</design>

The lstCountries node is list of iso country codes but sometimes they are
not sorted alphabetically and sometimes I don't want them all to be visible,
so I'm writing an app to resolve these problems. BUT I'm failing at the
first hurdle because I can't see how to read the config file and select JUST
the lstCountries values, putting them into a CheckedListBox.

If the node was simply called <countries> then I can select it but because
it's <type name=".... I can't figure out how to select just that section of
the file.

I suppose I'll have the same problem with the <value="... elements as well
but if I can solve the first problem I should be OK with that one!

Can anyone give me some pointers? Or better still, is there any code
available? I've trawled everywhere with no success!

Thanks.

JB
 
R

Rick

xmlDocument.DocumentElement.SelectSingleNode("types/type[@name='1stCountries']")

Not tested but it should be something like that

Rick
 
F

FishingScout

John,

First your xml is poorly formed. It is missing the closing </types>
before the </design> tag.

Next, to query the values node for the type whos name is "lstcountries"
use the following:

xmldocument.SelectSingleNode(
"/design/types/type[@name='lstCountries']/values")

or less specifically:

xmldocument.SelectSingleNode("//type[@name='lstCountries']/values")

That will return the the values node.

However, it seems to me that you really want to get all the country
codes. To do that you should get a node array of all the country code
values. Use something like:

Dim CountryCodeValueNodes As XmlNodeList =
xmldocument.SelectNodes("//type[@name='lstCountries']/values/value")

For Each CountryCodeValue as Node in CountryCodeValueNodes
Dim CountryCode as String = CountryCodeValue.Attributes("key")
' do something with the country code ....
Next
 
J

John Barleycorn

Hi, and thank you, FishingScout

I have now created the following code:

Dim configurationFile As New XmlDocument()

configurationFile.Load("C:\temp\webdir4\design2.xml")


Dim CountryCodeValueNodes As XmlNodeList

CountryCodeValueNodes =
configurationFile.SelectNodes("//type[@name='lstCountries']/values/value")

Dim CountryCodeValue As XmlNode

For Each CountryCodeValue In CountryCodeValueNodes

Dim CountryCode As String = CountryCodeValue.Attributes("key")

'do something with the country code ....

CheckedListBox1.Items.Add(CountryCode)

Next

However, when I try to run the code I receive the error: "Value of type
'System.Xml.XmlAttribute' cannot be converted to 'String'." referring to the
line "Dim CountryCode As String = CountryCodeValue.Attributes("key")"

What should I be doing to get rid of this?

Thanks for your help! One day I will give VB the time it deserves to
actually learn the fundamentals.

JB
 
J

John Barleycorn

In fact, I'm obviously a muppet!

If I change the code to the following that error disappears:
Dim configurationFile As New XmlDocument()

configurationFile.Load("C:\temp\config\design2.xml")


Dim CountryCodeValueNodes As XmlNodeList

CountryCodeValueNodes =
configurationFile.SelectNodes("//type[@name='lstCountry']/values/value")

Dim CountryCodeValue As XmlNode

For Each CountryCodeValue In CountryCodeValueNodes

Dim CountryCode = CountryCodeValue.Attributes("key")

'do something with the country code ....

CheckedListBox1.Items.Add(CountryCode)

Next

The problem is that now when I actually run the code the CheckedListBox is
filled with entries, all identical with the value 'System.Xml.XmlAttribute'
rather than US, AE.... etc

ANy ideas?



JB
 
N

Norman Chong

John said:
For Each CountryCodeValue In CountryCodeValueNodes

Dim CountryCode = CountryCodeValue.Attributes("key")

'do something with the country code ....

CheckedListBox1.Items.Add(CountryCode)

Next

The problem is that now when I actually run the code the CheckedListBox is
filled with entries, all identical with the value 'System.Xml.XmlAttribute'
rather than US, AE.... etc

Hi John,
The reason why this "works" is, that you late-bind the variable
CountryCode, that means that you don't specify a type (which is a bad
idea in this case). I assume your compiler is warning you to do this??
You have to use the type Xml.XmlNode for CountryCode.
Your problem is, that you try to add an Xml.XmlAttribute Object to your
list - The object will be casted to a string and only the classname of
the object is shown ('System.Xml.XmlAttribute'). To get this working
you have to change CheckedListBox1.Items.Add(CountryCode) to
CheckedListBox1.Items.Add(CountryCode.value)

Now my 2 cents: It seems that you really need some knowledge about the
basics, so I would recommend that you first learn them before
continuing - This will spare you a lot of pain ;-)
 
N

Norman Chong

Norman said:
The reason why this "works" is, that you late-bind the variable
CountryCode

Wrong word for this, sorry. This is no late-binding, it's just a
variable declaration without a type ;-)
 
J

John Barleycorn

Hi Norman

Of course you're right about starting somewhere else etc, but I always find
it best to dig deep and start a project based on real issues rather than
start from the beginning. Works for me - you may differ!

That aside, I now have a great piece of code that does exactly what I need
it to do for now. I'll certainly be following up a bit more on the basics
when I get a bit more time - thanks for your help!
 
F

FishingScout

John,

I missed a bit of code. It should have read like this:

Dim CountryCode As String =
CountryCodeValue.Attributes("key").InnerText

Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top