Vb.net 读取具有相同元素名称的XMLDocument

Vb.net 读取具有相同元素名称的XMLDocument,vb.net,Vb.net,我必须阅读以下XML文档数据: <root> <cartype>Mercedes</cartype> <production>2005</production> <fuel>Diesel</fuel> <color>Red</color> <services> <service> <year>2006</ye

我必须阅读以下XML文档数据:

<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>

读取具有唯一元素名称的节点是可以的,但我不知道如何读取数组中的所有“服务”,因为显示的代码只读取第一个服务。服务的数量可以从0到未定义。函数必须尽可能快,因为必须在尽可能短的时间内读取大量xml。

尝试linq解析xml会更容易: 必须是这样的:

xelement = XElement.Load(file);
services = xelement.Element("services").Elements("service");

要读取数量可变的
元素,您需要使用
SelectNodes
而不是
SelectSingleNode

Dim services = root.SelectNodes("services/service")
然后,您可以迭代
节点:

For Each service In services
    If service("year") IsNot Nothing Then
       Dim year = service("year").InnerText
    End If
Next
就个人而言,我会使用解析文件(但这可能是因为我对LINQ的所有东西都很着迷!)。结合VB.NET对它的支持,可以生成一些非常好看的代码(IMHO)

下面是一个完整的示例,您可以粘贴到其中

Sub-Main
'使用XElememt.Load(文件名)从文件中加载
dimXML=
梅赛德斯
2005
柴油机
红色
2006
47800
斯图加特
2007
92125
FFM
180420
2009
汉诺威
好
Dim history=新的ServiceHistory(),带有{
.CarType=xml..Value,
.Production=xml..Value,
.Fuel=xml..Value,
.Color=xml..Value,
.Condition=xml..Value,
.服务=(
从xml中的svc。。
选择新服务(),然后单击{
.Year=svc..Value,
.KM=svc..值,
.City=svc..Value
}
)托利斯先生()
}
history.Dump()
端接头
'在此处定义其他方法和类
公共课历史
公共属性类型为字符串
作为字符串的公共财产生产
作为字符串的公共财产
公共属性颜色作为字符串
作为字符串的公共属性条件
公共财产服务(服务清单)
末级
公务舱服务
公共财产年作为字符串
公共财产作为字符串
作为字符串的公共财产城市
末级
这将为您提供以下信息:


感谢Mark提供了极具教育意义的答案和可行、便捷的解决方案。我更喜欢老式的方式,而且很好用。只需更改这一行:“对于服务中作为XmlNode的每个服务”,可能是因为Option Strict已打开。很高兴它很有用。对于
As XmlNode
部分,我总是使用
选项expert On
选项Strict On
,只是为了在保持严格类型的同时避免少量键入,所以我总是忘记在示例中显式-对此感到抱歉。
For Each service In services
    If service("year") IsNot Nothing Then
       Dim year = service("year").InnerText
    End If
Next
Sub Main
    ' Use XElememt.Load(fileName) to load from file
    Dim xml =
    <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 history = New ServiceHistory() With {
        .CarType = xml.<cartype>.Value,
        .Production = xml.<production>.Value,
        .Fuel = xml.<fuel>.Value,
        .Color = xml.<color>.Value,
        .Condition = xml.<condition>.Value,
        .Services = (
            From svc In xml.<services>.<service>
            Select New Service() With {
                .Year = svc.<year>.Value,
                .KM = svc.<km>.Value,
                .City = svc.<city>.Value
            }
        ).ToList()
    }
    history.Dump()
End Sub

' Define other methods and classes here
Public Class ServiceHistory
    Public Property CarType As String
    Public Property Production As String
    Public Property Fuel As String
    Public Property Color As String
    Public Property Condition As String
    Public Property Services As List(Of Service)
End Class

Public Class Service
    Public Property Year As String
    Public Property KM As String
    Public Property City As String
End Class