|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Selecting a single node in 'XML'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 xmlDocument.DocumentElement.SelectSingleNode("types/type[@name='1stCountries']")
Not tested but it should be something like that Rick Show quoteHide quote "John Barleycorn" <john.barleycorn@nospam.com> wrote in message news:ubff6wr%23GHA.1224@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 <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 > 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 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 Show quoteHide quote "FishingScout" <fishingsc***@comcast.net> wrote in message news:1162064444.408677.229710@k70g2000cwa.googlegroups.com... > 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 > 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 John Barleycorn schrieb:
Show quoteHide quote > For Each CountryCodeValue In CountryCodeValueNodes Hi John,> > 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 > 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 ;-) Norman Chong schrieb:
> The reason why this "works" is, that you late-bind the variable Wrong word for this, sorry. This is no late-binding, it's just a> CountryCode variable declaration without a type ;-) 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! Show quoteHide quote "Norman Chong" <normanch***@freenet.de> wrote in message news:1162217352.802494.174410@b28g2000cwb.googlegroups.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 ;-) > > 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: Show quoteHide quote > 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" <normanch***@freenet.de> wrote in message > news:1162217352.802494.174410@b28g2000cwb.googlegroups.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 ;-) > > > >
Constants can't be defined in a namespace. WTH?
Math class for Visual Basic.NET (matrix operations) Graph control webreq.getresponse exceptions Simple thread implementation not working. Errors in Page directive turn off assertions in VB 2005 Adding a Web Reference into a Windows Project Determine if xml is malformed Date from Double |
|||||||||||||||||||||||