读取 XML (VB.net) 中的所有节点和元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17025545/
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
Read all nodes and elements in XML (VB.net)
提问by gubbfett
I'm trying to read an xml file, and that's usually no problems.
我正在尝试读取 xml 文件,这通常没有问题。
But, in this case I will not know anything about this xml file, i just want to read everything, including all child nodes and get the name and value from each node.
但是,在这种情况下,我对这个 xml 文件一无所知,我只想读取所有内容,包括所有子节点并从每个节点获取名称和值。
This code gives me only the name of the first node and skips all children:
这段代码只给了我第一个节点的名称并跳过所有子节点:
Dim xml As New XmlDocument
xml.Load(myxml.xml)
For Each node As XmlNode In xml.DocumentElement.SelectNodes("*")
MsgBox(node.Name)
Next
since i don't know dept or anything, i don't know how i will do this. And every solution i find is based by knowing element names.
因为我不知道部门或任何事情,所以我不知道我将如何做到这一点。我找到的每个解决方案都基于了解元素名称。
回答by SysDragon
Another option:
另外一个选项:
Dim xml As New Xml.XmlTextReader(sFilePath)
While xml.Read
If xml.NodeType = Xml.XmlNodeType.Element Then
MessageBox.Show(xml.Name)
End If
End While

