Simple XML problem :)

If the following document is saved as "example.xml":

</ xml>
<nodeA>
<nodeB>
<nodeC>
<nodeD>
<value1>NO</value1>
<value2>NO</value2>
<value3>NO</value3>
<value4>YES</value4>
</nodeD>
</nodeC>
</nodeB>
</nodeA>

How can I then get value4

My code works along the following lines:

Dim xml_doc As XmlDocument
Dim xml_nodelist As XmlNodeList
Dim xml_node As XmlNode

xml_doc = New XmlDocument()

Dim xml_reader As XmlTextReader = Nothing
xml_reader = New xmlTextReader("example.xml")
xml_doc.Load(xmlReader)

xml_nodelist = xml_doc.SelectNodes("/nodeA/nodeB/nodeC/nodeD")

What can I do from here



Answer this question

Simple XML problem :)

  • Lauriew

    Dim xml_doc As XmlDocument

    Dim xml_nodelist As XmlNodeList

    Dim xml_node As XmlNode

    xml_doc = New XmlDocument()

    Dim xml_reader As XmlTextReader = Nothing

    xml_reader = New XmlTextReader("example.xml")

    ' note incorrect syntax in your post must provide instantiated object not object type

    xml_doc.Load(xml_reader)

    xml_nodelist = xml_doc.SelectNodes("/nodeA/nodeB/nodeC/nodeD")

    If xml_nodelist.Count >= 4 Then

    xml_node = xml_nodelist(3)

    Dim ValueOfnode4 As String = xml_node.Value

    ' Provided for breakpoint

    ValueOfnode4 = String.Empty

    End If

    ' or using the xmlreader

    ' Loop throuigh each node getting the value

    While xml_reader.Read()

    If xml_reader.HasValue Then

    Dim ans As String = xml_reader.Value

    ' provide for breakpoint

    ans = String.Empty

    End If

    End While

    rgds,

    Martin


  • calmal20

    Try this..note you want to get down to the node level and not the list. To get to the node...using Xpath

    node as XmlNode = xml_doc.SelectSingleNode(@"descendant::value4")

    if (Not(node is Nothing))
       MessageBox.Show(node.InnerText) ' Prints out "Yes"




  • Simple XML problem :)