我要读XML文档数据,是在这里:阅读的XMLDocument具有相同的元素名称
<root>
<cartype>Mercedes</cartype>
<production>2005</production>
<fuel>Diesel</fuel>
<color>Red</color>
<services>
<service>
<year>2006</year>
<km>47800</km>
<city>Stuttgart</city>
</service>
<service>
<year>2007</year>
<km>92125</km>
<city>FFM</city>
</service>
<service>
<km>180420</km>
<year>2009</year>
<city>Hannover</city>
</service>
</services>
<condition>Good</condition>
</root>
然后我读它是这样:
Dim cartype As String
Dim fuel As String
Dim production As String
Dim color As String
Dim serviceyear() As String = {"", "", ""}
Dim servicekm() As String = {"", "", ""}
Dim sevicecity() As String = {"", "", ""}
Dim doc As XmlDocument = New XmlDocument()
doc.Load("mercedes.xml")
Dim root As XmlElement = doc.DocumentElement
Dim node As XmlNode = root.SelectSingleNode("production")
If Not node Is Nothing Then production = node.InnerText
node = root.SelectSingleNode("cartype")
If Not node Is Nothing Then cartype = node.InnerText
node = root.SelectSingleNode("fuel")
If Not node Is Nothing Then fuel = node.InnerText
node = root.SelectSingleNode("color")
If Not node Is Nothing Then color = node.InnerText
node = root.SelectSingleNode("services/service/year") '' first service year
If Not node Is Nothing Then serviceyear(0) = node.InnerText
node = root.SelectSingleNode("services/service/year") '' second service year
If Not node Is Nothing Then serviceyear(1) = node.InnerText
具有独特的元素名称读节点是确定的,但我不知道如何读取数组中的所有“服务”,因为显示的代码只读取第一个服务。服务可能从0到未定义的数字。函数必须尽可能快,因为大量的xml必须在可能的时间内被减少。
谢谢你马克非常教育答案和可行的,方便的解决方案。我更喜欢oldfashion的方式,它很好。只需要改变这一行:“对于每个服务作为XmlNode在服务中”可能是因为Option Strict开启。 – 2014-11-14 19:23:51
很高兴有用。对于'作为XmlNode'部分,我总是有'Option Infer On'和'Option Strict On',以避免在严格打字的同时轻轻打字,所以我总是忘记在例子中明确表达 - 对此深表歉意。 – Mark 2014-11-14 19:34:09