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. How to get selected node in a XML with childnodes and childnodes' childnodes?

How to get selected node in a XML with childnodes and childnodes' childnodes?

Scheduled Pinned Locked Moved Visual Basic
tutorialxmlhelpquestion
3 Posts 2 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.
  • S Offline
    S Offline
    sanyexian
    wrote on last edited by
    #1

    Hi,everyone!I have a XML, its structure was like below: <Height> <Standard>4721 <PN>1.6 <A DN="250">300</A> <A DN="350">400</A> <A DN="450">500</A> <A DN="550">600</A> </PN> <PN>2.5 <A DN="250">300</A> <A DN="350">400</A> <A DN="450">500</A> <A DN="550">600</A> </PN> </Standard> </Height> I want to get the selected node's text,for example,when PN = 1.6,DN = 250,the selected node's text was 300;PN=2.5,DN=350,the text should be 400.I tried use "for each node in childnodes",but it returned "1.6 300400500600".How could get the node I wanted? I think that should be first selected "PN",and then get each childnode in . But I don't know how to do this :sigh: PLZ give me some help,thx!

    E 1 Reply Last reply
    0
    • S sanyexian

      Hi,everyone!I have a XML, its structure was like below: <Height> <Standard>4721 <PN>1.6 <A DN="250">300</A> <A DN="350">400</A> <A DN="450">500</A> <A DN="550">600</A> </PN> <PN>2.5 <A DN="250">300</A> <A DN="350">400</A> <A DN="450">500</A> <A DN="550">600</A> </PN> </Standard> </Height> I want to get the selected node's text,for example,when PN = 1.6,DN = 250,the selected node's text was 300;PN=2.5,DN=350,the text should be 400.I tried use "for each node in childnodes",but it returned "1.6 300400500600".How could get the node I wanted? I think that should be first selected "PN",and then get each childnode in . But I don't know how to do this :sigh: PLZ give me some help,thx!

      E Offline
      E Offline
      Eaverae
      wrote on last edited by
      #2

      System.Xml has a method SelectSingleNode, which requires an xPath expression in order to work. Your method should look somewhat like the following:

      Sub GetSelected(string _PN, string_ DN)

      ' Put some code here to read your XML document into a XMLDocument variable,
      ' called XDoc for instance.

      XmlNode _XN = XDoc.SelectSingleNode("/Height/Standard/" + _PN + "/A")

      End Sub

      (yes, I'm not a hero in vb, 5 pts if you guess my 'dialect'). Hope this steers you in the right direction!

      "My personality is not represented by my hometown."

      S 1 Reply Last reply
      0
      • E Eaverae

        System.Xml has a method SelectSingleNode, which requires an xPath expression in order to work. Your method should look somewhat like the following:

        Sub GetSelected(string _PN, string_ DN)

        ' Put some code here to read your XML document into a XMLDocument variable,
        ' called XDoc for instance.

        XmlNode _XN = XDoc.SelectSingleNode("/Height/Standard/" + _PN + "/A")

        End Sub

        (yes, I'm not a hero in vb, 5 pts if you guess my 'dialect'). Hope this steers you in the right direction!

        "My personality is not represented by my hometown."

        S Offline
        S Offline
        sanyexian
        wrote on last edited by
        #3

        Hello,Eaverae! Your suggestion is helpful for me to get more details about the method "SelectedSingleNode".Thank you! I have got my solution like below:

        For Each node1 In list1
        Dim val As String
        val = node1.Attributes("PN").Value
        If val = TextBox1.Text Then
        Dim list2 As XmlNodeList = node1.SelectNodes("Height")
        Dim node2 As XmlNode
        For Each node2 In list2
        Dim val2 As String
        val2 = node2.Attributes("DN").Value
        If val2 = TextBox2.Text Then
        TextBox3.Text = node2.InnerText
        End If
        Next
        End If
        Next

        What's more, I have change the XML element

        <PN>

        to

        <PN PN="1.6">

        :)

        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