Michael Michael - 1 year ago 85
Vb.net Question

Select all Nodes Innertext any depth

I have a problem that I'm thinking about for hours now. Maybe I just think the wrong way ... Whatever:

I have multiple XML Documents that I search for (recursive file search) and load as "XmlDocument"

Dim doc As XmlDocument = New XmlDocument()
doc.Load(New StreamReader(File.Open(strFilePathAndName, FileMode.Open), Encoding.GetEncoding("iso-8859-15")))
Dim root As XmlElement = doc.DocumentElement

Then I have to search in this document for every child note, but not it's "parent"
A bit hard to explain ... Well as example:

<?xml version="1.0"?>
<node1>Users</node1> '<- Get "Users" from that
<node2>xxxxxx</node2> '<- Get "xxxxxxx" from that
<node3> '<- Don't get that
<node4> '<- Don't get that
<nodeX>xxxxxx</nodeX> '<- Get xxxxx that
<node5> '<- Don't get that
<bla>335</bla> '<- Get 335 fromthat
<bla2>3353</bla2> '<- Get 3353 from that

Original is a bit larger...

I tried saving them in a NodeList:

nodeList = root.SelectNodes("descendant::*")

And then saving all found items to a string, seperated by ";" :

For Each node As XmlNode In nodeList
ergstring = ergstring & """" & node.InnerText & """" & ";"

Answer Source

Use nodeList = root.SelectNodes("descendant::*[not(*)]") to select all elements not having a child element.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download