vb.net 遍历 xml 文件的所有节点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20055689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 15:52:36  来源:igfitidea点击:

iterate through all nodes of an xml file

xmlvb.net

提问by user1361914

<ArrayOfContacts xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="someschema">
 <Contact>
    <ID />
    <First_Name />
    <Last_Name />
    <TelephoneNumbers>
        <TelephoneNumber>
            <Number />
            <IsHome />
            <IsWork />
            <IsCell />
            <ReachableAfterHrs />
        </TelephoneNumber>
    </TelephoneNumbers>
 </Contact>
  <Contact>
    <ID />
    <First_Name />
    <Last_Name />
    <TelephoneNumbers>
        <TelephoneNumber>
            <Number />
            <IsHome />
            <IsWork />
            <IsCell />
            <ReachableAfterHrs />
        </TelephoneNumber>
    </TelephoneNumbers>
 </Contact>
</ArrayOfContacts>

Looked at this article. Looking for a good way to go over the entire xml and change all node values that need to be changed, this would be dynamically chosen and then to save the document

看了这篇文章。寻找一种遍历整个xml并更改所有需要更改的节点值的好方法,这将被动态选择然后保存文档

My recursive routine is similar to this

我的递归例程与类似

however when it encounters <TelephoneNumbers>it does not go down deeper to get the individual elements.

然而,当它遇到时,<TelephoneNumbers>它不会深入获取单个元素。

My fn to recurse thru the XMl

我的 fn 通过 XML 递归

Protected Sub RecurseXML(nodes As XmlNodeList)
    For Each node As XmlNode In nodes
        If (node.ChildNodes.Count > 1) Then
            RecurseXML(node.ChildNodes)
        Else
            node.InnerText = ChangeNodeValue()
        End If
    Next
End Sub

Basically, trying to read entire XML and change certain vlues[node names not known] and then save the update document.

基本上,尝试读取整个 XML 并更改某些 vlues[节点名称未知],然后保存更新文档。

采纳答案by Szymon

Your code doesn't go into <TelephoneNumbers>because of line

<TelephoneNumbers>由于线路,您的代码没有进入

If (node.ChildNodes.Count > 1) Then

There's only one child element here so it stops at that level.

这里只有一个子元素,所以它停在那个级别。