Selecting a single node in 'XML'

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Barleycorn

    Selecting a single node in 'XML'

    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="Passw ord" 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="lstCountr ies" 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 <countriesthe n 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


  • Rick

    #2
    Re: Selecting a single node in 'XML'

    xmlDocument.Doc umentElement.Se lectSingleNode( "types/type[@name='1stCount ries']")

    Not tested but it should be something like that

    Rick


    "John Barleycorn" <john.barleycor n@nospam.comwro te in message
    news:ubff6wr%23 GHA.1224@TK2MSF TNGP04.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="Passw ord" 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="lstCountr ies" 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 <countriesthe n 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
    >

    Comment

    • FishingScout

      #3
      Re: Selecting a single node in 'XML'

      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 "lstcountri es"
      use the following:

      xmldocument.Sel ectSingleNode(
      "/design/types/type[@name='lstCount ries']/values")

      or less specifically:

      xmldocument.Sel ectSingleNode("//type[@name='lstCount ries']/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 CountryCodeValu eNodes As XmlNodeList =
      xmldocument.Sel ectNodes("//type[@name='lstCount ries']/values/value")

      For Each CountryCodeValu e as Node in CountryCodeValu eNodes
      Dim CountryCode as String = CountryCodeValu e.Attributes("k ey")
      ' do something with the country code ....
      Next

      Comment

      • John Barleycorn

        #4
        Re: Selecting a single node in 'XML'

        Hi, and thank you, FishingScout

        I have now created the following code:

        Dim configurationFi le As New XmlDocument()

        configurationFi le.Load("C:\tem p\webdir4\desig n2.xml")


        Dim CountryCodeValu eNodes As XmlNodeList

        CountryCodeValu eNodes =
        configurationFi le.SelectNodes( "//type[@name='lstCount ries']/values/value")

        Dim CountryCodeValu e As XmlNode

        For Each CountryCodeValu e In CountryCodeValu eNodes

        Dim CountryCode As String = CountryCodeValu e.Attributes("k ey")

        'do something with the country code ....

        CheckedListBox1 .Items.Add(Coun tryCode)

        Next

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

        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




        "FishingSco ut" <fishingscout@c omcast.netwrote in message
        news:1162064444 .408677.229710@ k70g2000cwa.goo glegroups.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 "lstcountri es"
        use the following:
        >
        xmldocument.Sel ectSingleNode(
        "/design/types/type[@name='lstCount ries']/values")
        >
        or less specifically:
        >
        xmldocument.Sel ectSingleNode("//type[@name='lstCount ries']/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 CountryCodeValu eNodes As XmlNodeList =
        xmldocument.Sel ectNodes("//type[@name='lstCount ries']/values/value")
        >
        For Each CountryCodeValu e as Node in CountryCodeValu eNodes
        Dim CountryCode as String = CountryCodeValu e.Attributes("k ey")
        ' do something with the country code ....
        Next
        >

        Comment

        • John Barleycorn

          #5
          Re: Selecting a single node in 'XML'

          In fact, I'm obviously a muppet!

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

          configurationFi le.Load("C:\tem p\config\design 2.xml")


          Dim CountryCodeValu eNodes As XmlNodeList

          CountryCodeValu eNodes =
          configurationFi le.SelectNodes( "//type[@name='lstCount ry']/values/value")

          Dim CountryCodeValu e As XmlNode

          For Each CountryCodeValu e In CountryCodeValu eNodes

          Dim CountryCode = CountryCodeValu e.Attributes("k ey")

          'do something with the country code ....

          CheckedListBox1 .Items.Add(Coun tryCode)

          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.Xml Attribute'
          rather than US, AE.... etc

          ANy ideas?



          JB


          Comment

          • Norman Chong

            #6
            Re: Selecting a single node in 'XML'


            John Barleycorn schrieb:
            For Each CountryCodeValu e In CountryCodeValu eNodes
            >
            Dim CountryCode = CountryCodeValu e.Attributes("k ey")
            >
            'do something with the country code ....
            >
            CheckedListBox1 .Items.Add(Coun tryCode)
            >
            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.Xml Attribute'
            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.XmlAttribut e Object to your
            list - The object will be casted to a string and only the classname of
            the object is shown ('System.Xml.Xm lAttribute'). To get this working
            you have to change CheckedListBox1 .Items.Add(Coun tryCode) to
            CheckedListBox1 .Items.Add(Coun tryCode.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 ;-)

            Comment

            • Norman Chong

              #7
              Re: Selecting a single node in 'XML'


              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 ;-)

              Comment

              • John Barleycorn

                #8
                Re: Selecting a single node in 'XML'

                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" <normanchong@fr eenet.dewrote in message
                news:1162217352 .802494.174410@ b28g2000cwb.goo glegroups.com.. .
                >
                John Barleycorn schrieb:
                >
                >For Each CountryCodeValu e In CountryCodeValu eNodes
                >>
                >Dim CountryCode = CountryCodeValu e.Attributes("k ey")
                >>
                >'do something with the country code ....
                >>
                >CheckedListBox 1.Items.Add(Cou ntryCode)
                >>
                >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.Xm lAttribute'
                >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.XmlAttribut e Object to your
                list - The object will be casted to a string and only the classname of
                the object is shown ('System.Xml.Xm lAttribute'). To get this working
                you have to change CheckedListBox1 .Items.Add(Coun tryCode) to
                CheckedListBox1 .Items.Add(Coun tryCode.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 ;-)
                >
                >

                Comment

                • FishingScout

                  #9
                  Re: Selecting a single node in 'XML'

                  John,

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

                  Dim CountryCode As String =
                  CountryCodeValu e.Attributes("k ey").InnerTex t

                  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" <normanchong@fr eenet.dewrote in message
                  news:1162217352 .802494.174410@ b28g2000cwb.goo glegroups.com.. .

                  John Barleycorn schrieb:
                  For Each CountryCodeValu e In CountryCodeValu eNodes
                  >
                  Dim CountryCode = CountryCodeValu e.Attributes("k ey")
                  >
                  'do something with the country code ....
                  >
                  CheckedListBox1 .Items.Add(Coun tryCode)
                  >
                  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.Xml Attribute'
                  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.XmlAttribut e Object to your
                  list - The object will be casted to a string and only the classname of
                  the object is shown ('System.Xml.Xm lAttribute'). To get this working
                  you have to change CheckedListBox1 .Items.Add(Coun tryCode) to
                  CheckedListBox1 .Items.Add(Coun tryCode.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 ;-)

                  Comment

                  Working...