Looping through all elements in XML VB.NET

时间:2016-11-03 15:40:33

标签: .net xml

I was wondering if there is any way to loop through all elements in an XML using XmlDocument and storing the elements in a String array. I want to be able to do this with any XML that contains elements. All of the answers I've seen here so far, are made for specific XML files. I would like to do this with any XML in VB.NET.

1 个答案:

答案 0 :(得分:1)

这可以通过枚举Descendants()的所有XDocument来轻松完成:

Module Program

    Sub Main()

        Dim xDocument = <?xml version="1.0"?>
                        <root>
                            <node1>
                                <node2></node2>
                            </node1>
                            <node1>
                                <node2></node2>
                            </node1>
                        </root>

        For Each el In xDocument.Descendants()
            Console.WriteLine(el.Name)
        Next

    End Sub

End Module

您可以从VB XML文字(如上所述),字符串(XDocument或文件/流(XDocument.Parse("<root></root>"))创建XDocument.Load(fileName)的实例。

相关问题