By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,640 Members | 1,363 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,640 IT Pros & Developers. It's quick & easy.

Selecting a single node in 'XML'

P: n/a
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 <countriesthen 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
Oct 28 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
xmlDocument.DocumentElement.SelectSingleNode("type s/type[@name='1stCountries']")

Not tested but it should be something like that

Rick
"John Barleycorn" <jo*************@nospam.comwrote in message
news:ub****************@TK2MSFTNGP04.phx.gbl...
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 <countriesthen 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

Oct 28 '06 #2

P: n/a
John,

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

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

Oct 28 '06 #3

P: n/a
Hi, and thank you, FishingScout

I have now created the following code:

Dim configurationFile As New XmlDocument()

configurationFile.Load("C:\temp\webdir4\design2.xm l")
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


"FishingScout" <fi**********@comcast.netwrote in message
news:11**********************@k70g2000cwa.googlegr oups.com...
John,

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

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

Oct 30 '06 #4

P: n/a
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
Oct 30 '06 #5

P: n/a

John Barleycorn schrieb:
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 ;-)

Oct 30 '06 #6

P: n/a

Norman Chong schrieb:
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 ;-)

Oct 30 '06 #7

P: n/a
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!
"Norman Chong" <no*********@freenet.dewrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
>
John Barleycorn schrieb:
>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 ;-)


Oct 30 '06 #8

P: n/a
John,

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

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

Steve

John Barleycorn wrote:
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!
"Norman Chong" <no*********@freenet.dewrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...

John Barleycorn schrieb:
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 ;-)
Oct 30 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.