Hi All
I am trying to get vb.net to select a single node by id. I then want get all of the childnodes within the id and place them in to textboxes. Please see code below:

Xml doc

<Subject>
  <Items>
    <Item id="1">
      <CustName>john</CustName>
      <Filename>
      </Filename>
      <StartDate>
      </StartDate>
      <FinishDate>
      </FinishDate>
      <Cost>
      </Cost>
      <PayFrequency>
      </PayFrequency>
      <Type>Video</Type>
      <File>c:\test.avi</File>
      <Time>6</Time>
      <TimeFrom>181</TimeFrom>
    </Item>
    <Item id="2">
      <CustName>
      </CustName>
      <Filename>...........................................

Vb code

Dim doc As New XmlDocument()
 
 doc.Load(module1.path)

 
 Dim n As XmlNode = doc.SelectSingleNode("/Subject/Items/Item[@ID='1']")
 TextBox1.Text = n("CustName").InnerText

Have scoured google and Daniweb forums but cannot find the answer im looking for.

Thanks for your help in advance

John

Recommended Answers

All 2 Replies

See if this helps.
1 Button

Imports System.Xml
Public Class Form1
    Private myXML_FileLocation As String = "C:\test.xml" '// .xml file and location.
    Private xmlDoc As New XmlDocument, xmlSelectedNode As XmlNode

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        xmlDoc.Load(myXML_FileLocation) 'Load the Xml file once if constantly in use.
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        getXMLnodeByID("2")
    End Sub
    Private Sub getXMLnodeByID(ByVal IDToUse As String)
        xmlSelectedNode = xmlDoc.SelectSingleNode("Subject/Items/Item[@id='" & IDToUse & "']")
        With xmlSelectedNode
            MsgBox(.SelectSingleNode("CustName").InnerText)
            MsgBox(.SelectSingleNode("Filename").InnerText)
            MsgBox(.SelectSingleNode("StartDate").InnerText)
            '// etc...
        End With
    End Sub
End Class

I noticed that your .xml file has <Item id="1"> which is all LowerCase and you were using ("/Subject/Items/Item[@ID='1']") .ToUpper, which will result in errors. At least it did on my part.

Thanks codeorder

Just found another way about 2 hours ago that works well. I have posted it below for anyone elses reference

Dim doc As New XmlDocument()
        Dim nodes As XmlNodeList

        doc.Load(path)
        nodes = doc.SelectNodes("/Subject/Items/Item[@id=" & Module1.Client_id & "]")

        Dim node As XmlNode
        For Each node In nodes
            TextBox1.Text = node.SelectSingleNode("CustName").InnerText
            TextBox4.Text = node.SelectSingleNode("PayFrequency").InnerText
            TextBox5.Text = node.SelectSingleNode("Cost").InnerText
        Next

Much appreciated for your time and effort

John

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.