Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Retrieving value from an XML file

Retrieving value from an XML file

Scheduled Pinned Locked Moved Visual Basic
csharpxmlhelp
12 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Ben Senior
    wrote on last edited by
    #1

    I'm new to working with XML in VB.NET and would like a little help after tearing my hair out for a couple of days. I have an XML file with the following structure:

    -<conceptGrp>
    -<descripGrp>
    <descrip type="subjectField">6411, 6821</descrip>
    </descripGrp>
    -<languageGrp>
    <language lang="DE" type="German"/>
    -<termGrp>
    <term>Scheren</term>
    -<descripGrp>
    <descrip type="termType">fullForm</descrip>
    </descripGrp>
    -<descripGrp>
    <descrip type="reliabilityCode">3</descrip>
    </descripGrp>
    </termGrp>
    </languageGrp>
    -<languageGrp>
    <language lang="EN" type="English"/>
    -<termGrp>
    <term>scissors</term>
    -<descripGrp>
    <descrip type="termType">fullForm</descrip>
    </descripGrp>
    -<descripGrp>
    <descrip type="reliabilityCode">3</descrip>
    </descripGrp>
    </termGrp>
    </languageGrp>
    </conceptGrp>

    First I need to cycle through all the elements in the file (>550000) and for each element extract the text "6411, 6821" from

    <descrip type="subjectField">6411, 6821</descrip>

    and "3" from

    <descrip type="reliabilityCode">3</descrip>

    The <descrpGrp> appears just once for each element. The <languageGrp> appears at least twice but up to 20 times. The <termGrp> appears mostly once but up to 10 times for each <languageGrp>. The <descrpGrp> appears twice for each <termGrp>. The <descrip type="reliabilityCode" ...> appears just once for each term. Using the extracted strings I look them up and need to replace them with text in the element. This is my code so far for getting the <descrip type="reliabilityCode">3</descrip>:

    ' loop thro each element in XML doc and replace the reliabiltiy codes
    For Each conceptGrp In xDoc.Elements("conceptGrp")
    For Each languageGrp In conceptGrp.Elements("languageGrp")
    For Each termGrp In languageGrp.Elements("termGrp")
    Dim code = termGrp.Element("reliabilityCode")
    For Each descripGP In termGrp.Elements("descripGrp")
    For Each descrip In descripGP.Elements("descrip")

    P D M 3 Replies Last reply
    0
    • B Ben Senior

      I'm new to working with XML in VB.NET and would like a little help after tearing my hair out for a couple of days. I have an XML file with the following structure:

      -<conceptGrp>
      -<descripGrp>
      <descrip type="subjectField">6411, 6821</descrip>
      </descripGrp>
      -<languageGrp>
      <language lang="DE" type="German"/>
      -<termGrp>
      <term>Scheren</term>
      -<descripGrp>
      <descrip type="termType">fullForm</descrip>
      </descripGrp>
      -<descripGrp>
      <descrip type="reliabilityCode">3</descrip>
      </descripGrp>
      </termGrp>
      </languageGrp>
      -<languageGrp>
      <language lang="EN" type="English"/>
      -<termGrp>
      <term>scissors</term>
      -<descripGrp>
      <descrip type="termType">fullForm</descrip>
      </descripGrp>
      -<descripGrp>
      <descrip type="reliabilityCode">3</descrip>
      </descripGrp>
      </termGrp>
      </languageGrp>
      </conceptGrp>

      First I need to cycle through all the elements in the file (>550000) and for each element extract the text "6411, 6821" from

      <descrip type="subjectField">6411, 6821</descrip>

      and "3" from

      <descrip type="reliabilityCode">3</descrip>

      The <descrpGrp> appears just once for each element. The <languageGrp> appears at least twice but up to 20 times. The <termGrp> appears mostly once but up to 10 times for each <languageGrp>. The <descrpGrp> appears twice for each <termGrp>. The <descrip type="reliabilityCode" ...> appears just once for each term. Using the extracted strings I look them up and need to replace them with text in the element. This is my code so far for getting the <descrip type="reliabilityCode">3</descrip>:

      ' loop thro each element in XML doc and replace the reliabiltiy codes
      For Each conceptGrp In xDoc.Elements("conceptGrp")
      For Each languageGrp In conceptGrp.Elements("languageGrp")
      For Each termGrp In languageGrp.Elements("termGrp")
      Dim code = termGrp.Element("reliabilityCode")
      For Each descripGP In termGrp.Elements("descripGrp")
      For Each descrip In descripGP.Elements("descrip")

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      If Only You Knew The Power Of The Dark Side XPath!

      B 1 Reply Last reply
      0
      • P PIEBALDconsult

        If Only You Knew The Power Of The Dark Side XPath!

        B Offline
        B Offline
        Ben Senior
        wrote on last edited by
        #3

        Not at all a helpful answer, but thanks for taking the time to write something :-(

        L 1 Reply Last reply
        0
        • B Ben Senior

          Not at all a helpful answer, but thanks for taking the time to write something :-(

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          The helpful bit was the suggestion to use XPath. If you look at the structure of the XML there is only ever one descrip element in each descripGrp, so maybe you are accessing it in the wrong way.

          B 1 Reply Last reply
          0
          • B Ben Senior

            I'm new to working with XML in VB.NET and would like a little help after tearing my hair out for a couple of days. I have an XML file with the following structure:

            -<conceptGrp>
            -<descripGrp>
            <descrip type="subjectField">6411, 6821</descrip>
            </descripGrp>
            -<languageGrp>
            <language lang="DE" type="German"/>
            -<termGrp>
            <term>Scheren</term>
            -<descripGrp>
            <descrip type="termType">fullForm</descrip>
            </descripGrp>
            -<descripGrp>
            <descrip type="reliabilityCode">3</descrip>
            </descripGrp>
            </termGrp>
            </languageGrp>
            -<languageGrp>
            <language lang="EN" type="English"/>
            -<termGrp>
            <term>scissors</term>
            -<descripGrp>
            <descrip type="termType">fullForm</descrip>
            </descripGrp>
            -<descripGrp>
            <descrip type="reliabilityCode">3</descrip>
            </descripGrp>
            </termGrp>
            </languageGrp>
            </conceptGrp>

            First I need to cycle through all the elements in the file (>550000) and for each element extract the text "6411, 6821" from

            <descrip type="subjectField">6411, 6821</descrip>

            and "3" from

            <descrip type="reliabilityCode">3</descrip>

            The <descrpGrp> appears just once for each element. The <languageGrp> appears at least twice but up to 20 times. The <termGrp> appears mostly once but up to 10 times for each <languageGrp>. The <descrpGrp> appears twice for each <termGrp>. The <descrip type="reliabilityCode" ...> appears just once for each term. Using the extracted strings I look them up and need to replace them with text in the element. This is my code so far for getting the <descrip type="reliabilityCode">3</descrip>:

            ' loop thro each element in XML doc and replace the reliabiltiy codes
            For Each conceptGrp In xDoc.Elements("conceptGrp")
            For Each languageGrp In conceptGrp.Elements("languageGrp")
            For Each termGrp In languageGrp.Elements("termGrp")
            Dim code = termGrp.Element("reliabilityCode")
            For Each descripGP In termGrp.Elements("descripGrp")
            For Each descrip In descripGP.Elements("descrip")

            D Offline
            D Offline
            David Mujica
            wrote on last edited by
            #5

            I'd like to help you, but I'm a bit unclear as to what you need to do. Do you want to take,

            3

            and change it to

            reliabilityCode

            and do the same for

            6411, 6821

            I think this can be done cleaner with xPath statements, rather then lots of looping. If my understanding is correct, then I will help with the xPath statements. :java:

            B 1 Reply Last reply
            0
            • D David Mujica

              I'd like to help you, but I'm a bit unclear as to what you need to do. Do you want to take,

              3

              and change it to

              reliabilityCode

              and do the same for

              6411, 6821

              I think this can be done cleaner with xPath statements, rather then lots of looping. If my understanding is correct, then I will help with the xPath statements. :java:

              B Offline
              B Offline
              Ben Senior
              wrote on last edited by
              #6

              Hi David, No. What I want to do is take: 3 and replace it with: Very reliable The "3" can vary and when I get this numerical code I have a function from where I can retrieve its text equivalent. I then need to insert the text equivalent back into the XML file. There are five of these numeric codes. I'm new to XML and have never used XPath before so I'm struggling at the moment. I've been researching XPath and LINQ and just can't seem to get the hang of them. I have been using the looping as that's what I'm used to doing in VB and C#. The last thing that I will need to do is go through each element and depending upon the value of the subject field write the whole element to a new file for that subject. But first thing first and replace the numeric codes with text. The subjectField is very similar other than there are over 700 subjects. I already have a function which converts the numeric subject field codes into text, and as above I need to insert the text back into the XML file.

              D 1 Reply Last reply
              0
              • L Lost User

                The helpful bit was the suggestion to use XPath. If you look at the structure of the XML there is only ever one descrip element in each descripGrp, so maybe you are accessing it in the wrong way.

                B Offline
                B Offline
                Ben Senior
                wrote on last edited by
                #7

                That is correct. There is in each element just one 6411, 6821. There are over 700 possible values for subjectField and there can be multiple values each separated by a comma. Her we have two values "6411" and "6821". In each element there are a minimum of two languageGrp and a maximum of thirty. It varies throughout the file. In each languageGrp there is at least one termGrp up to a maximum of 10. In each termGrp there is just one 3. There are up to five possible values of this code, here it is "3". I've never used XPath and have not much useful reference material at the moment. It's all new to me, I'm an old VB/C# horse.

                L 1 Reply Last reply
                0
                • B Ben Senior

                  Hi David, No. What I want to do is take: 3 and replace it with: Very reliable The "3" can vary and when I get this numerical code I have a function from where I can retrieve its text equivalent. I then need to insert the text equivalent back into the XML file. There are five of these numeric codes. I'm new to XML and have never used XPath before so I'm struggling at the moment. I've been researching XPath and LINQ and just can't seem to get the hang of them. I have been using the looping as that's what I'm used to doing in VB and C#. The last thing that I will need to do is go through each element and depending upon the value of the subject field write the whole element to a new file for that subject. But first thing first and replace the numeric codes with text. The subjectField is very similar other than there are over 700 subjects. I already have a function which converts the numeric subject field codes into text, and as above I need to insert the text back into the XML file.

                  D Offline
                  D Offline
                  David Mujica
                  wrote on last edited by
                  #8

                  See if this code helps. I realize that it doesn't do exactly what you want, but it shows how xPath works.

                  Dim xDoc As New XmlDocument
                  Dim xNodeList As XmlNodeList
                  Dim xNode As XmlNode

                      xDoc.Load("C:\\Temp\\test.xml")
                  
                      xNodeList = xDoc.SelectNodes("/conceptGrp/descripGrp/descrip\[@type='subjectField'\]")
                  
                      For Each xNode In xNodeList
                          xNode.InnerText = xNode.Attributes("type").Value
                          Debug.Print(xNode.InnerText)
                      Next
                  
                      xDoc.Save("C:\\Temp\\test\_1.xml")
                  
                  B 1 Reply Last reply
                  0
                  • B Ben Senior

                    That is correct. There is in each element just one 6411, 6821. There are over 700 possible values for subjectField and there can be multiple values each separated by a comma. Her we have two values "6411" and "6821". In each element there are a minimum of two languageGrp and a maximum of thirty. It varies throughout the file. In each languageGrp there is at least one termGrp up to a maximum of 10. In each termGrp there is just one 3. There are up to five possible values of this code, here it is "3". I've never used XPath and have not much useful reference material at the moment. It's all new to me, I'm an old VB/C# horse.

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    Ben Senior wrote:

                    not much useful reference material

                    Seriously? There is no end of it, from MSDN documentation to articles in both VB and C#.

                    1 Reply Last reply
                    0
                    • D David Mujica

                      See if this code helps. I realize that it doesn't do exactly what you want, but it shows how xPath works.

                      Dim xDoc As New XmlDocument
                      Dim xNodeList As XmlNodeList
                      Dim xNode As XmlNode

                          xDoc.Load("C:\\Temp\\test.xml")
                      
                          xNodeList = xDoc.SelectNodes("/conceptGrp/descripGrp/descrip\[@type='subjectField'\]")
                      
                          For Each xNode In xNodeList
                              xNode.InnerText = xNode.Attributes("type").Value
                              Debug.Print(xNode.InnerText)
                          Next
                      
                          xDoc.Save("C:\\Temp\\test\_1.xml")
                      
                      B Offline
                      B Offline
                      Ben Senior
                      wrote on last edited by
                      #10

                      I had to add the root to the code ie. "/mtf/conceptGrp/descripGrp/descrip[@type='subjectField']" and a variable codeID to hold the value from

                      codeID = xNode.InnerText

                      instead of your

                      xNode.InnerText

                      I replaced the codeID with codeText (the corresponding text from my function) and reinserted it into the xml file. I need to test out the subjectField, but I don't anticipate any difficulties because it basically the same as for reliabilityCode. Works a treat. Thanks for your help. Now I'm getting there with XPath :)

                      1 Reply Last reply
                      0
                      • B Ben Senior

                        I'm new to working with XML in VB.NET and would like a little help after tearing my hair out for a couple of days. I have an XML file with the following structure:

                        -<conceptGrp>
                        -<descripGrp>
                        <descrip type="subjectField">6411, 6821</descrip>
                        </descripGrp>
                        -<languageGrp>
                        <language lang="DE" type="German"/>
                        -<termGrp>
                        <term>Scheren</term>
                        -<descripGrp>
                        <descrip type="termType">fullForm</descrip>
                        </descripGrp>
                        -<descripGrp>
                        <descrip type="reliabilityCode">3</descrip>
                        </descripGrp>
                        </termGrp>
                        </languageGrp>
                        -<languageGrp>
                        <language lang="EN" type="English"/>
                        -<termGrp>
                        <term>scissors</term>
                        -<descripGrp>
                        <descrip type="termType">fullForm</descrip>
                        </descripGrp>
                        -<descripGrp>
                        <descrip type="reliabilityCode">3</descrip>
                        </descripGrp>
                        </termGrp>
                        </languageGrp>
                        </conceptGrp>

                        First I need to cycle through all the elements in the file (>550000) and for each element extract the text "6411, 6821" from

                        <descrip type="subjectField">6411, 6821</descrip>

                        and "3" from

                        <descrip type="reliabilityCode">3</descrip>

                        The <descrpGrp> appears just once for each element. The <languageGrp> appears at least twice but up to 20 times. The <termGrp> appears mostly once but up to 10 times for each <languageGrp>. The <descrpGrp> appears twice for each <termGrp>. The <descrip type="reliabilityCode" ...> appears just once for each term. Using the extracted strings I look them up and need to replace them with text in the element. This is my code so far for getting the <descrip type="reliabilityCode">3</descrip>:

                        ' loop thro each element in XML doc and replace the reliabiltiy codes
                        For Each conceptGrp In xDoc.Elements("conceptGrp")
                        For Each languageGrp In conceptGrp.Elements("languageGrp")
                        For Each termGrp In languageGrp.Elements("termGrp")
                        Dim code = termGrp.Element("reliabilityCode")
                        For Each descripGP In termGrp.Elements("descripGrp")
                        For Each descrip In descripGP.Elements("descrip")

                        M Offline
                        M Offline
                        Meshack Musundi
                        wrote on last edited by
                        #11

                        You can get and set the value using LINQ to XML and XML axis properties.

                        Dim conceptor = XDocument.Load("C:\file.xml")
                        ' Retrieve value
                        Dim subjectField = conceptor...<descrip>.Where(Function(d) d.@type = "subjectField").Value
                        ' Change value
                        conceptor...<descrip>.Where(Function(d) d.@type = "subjectField").Value = "1234"
                        ' Save changes
                        conceptor.Save("C:\file.xml")

                        The descendant axis property, ...<>, gets all descendants that have the name specified within the angle brackets. The attribute axis property, .@, returns a reference to the value of the specified attribute. The Value axis property returns a reference to the value of the first element in a collection of elements.

                        "As beings of finite lifespan, our contributions to the sum of human knowledge is one of the greatest endeavors we can undertake and one of the defining characteristics of humanity itself"

                        B 1 Reply Last reply
                        0
                        • M Meshack Musundi

                          You can get and set the value using LINQ to XML and XML axis properties.

                          Dim conceptor = XDocument.Load("C:\file.xml")
                          ' Retrieve value
                          Dim subjectField = conceptor...<descrip>.Where(Function(d) d.@type = "subjectField").Value
                          ' Change value
                          conceptor...<descrip>.Where(Function(d) d.@type = "subjectField").Value = "1234"
                          ' Save changes
                          conceptor.Save("C:\file.xml")

                          The descendant axis property, ...<>, gets all descendants that have the name specified within the angle brackets. The attribute axis property, .@, returns a reference to the value of the specified attribute. The Value axis property returns a reference to the value of the first element in a collection of elements.

                          "As beings of finite lifespan, our contributions to the sum of human knowledge is one of the greatest endeavors we can undertake and one of the defining characteristics of humanity itself"

                          B Offline
                          B Offline
                          Ben Senior
                          wrote on last edited by
                          #12

                          Thanks for that Meschack, As I said above I have solved that problem, with the help of people here, and now moved on to the next stage in the project, which is moving along quite well. I have noted your suggestions along with the others, for reference and future use.

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups