Reputation: 165
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.
Upvotes: 1
Views: 1318
Reputation: 176179
This can be easily done by enumerating all Descendants()
of an 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
You can create an instance of an XDocument
from a VB XML literal (as above), from a string (XDocument.Parse("<root></root>")
or from a file/stream (XDocument.Load(fileName)
).
Upvotes: 1