选择任何深度的所有节点Innertext

时间:2016-08-11 08:37:26

标签: xml vb.net string nodes nodelist

我有一个问题,我现在正在思考几个小时。也许我只是想错误的方式......无论如何:

我搜索了多个XML文档(递归文件搜索)并加载为“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

然后我必须在这个文件中搜索每个儿童笔记,但不是它的“父母” 有点难以解释......例如:

<?xml version="1.0"?>    
<rootnode>
    <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
            </node5>
        <node4>     
</rootnode>

原件有点大......

我尝试将它们保存在NodeList中:

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

然后将所有找到的项目保存到字符串中,用“;”分隔:

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

1 个答案:

答案 0 :(得分:0)

使用nodeList = root.SelectNodes("descendant::*[not(*)]")选择所有没有子元素的元素。