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

时间:2014-11-14 16:56:11

标签: vb.net

我必须阅读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 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

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

2 个答案:

答案 0 :(得分:2)

要阅读可变数量的<service>元素,您需要使用SelectNodes代替SelectSingleNode

Dim services = root.SelectNodes("services/service")

然后,您可以遍历<service>个节点:

For Each service In services
    If service("year") IsNot Nothing Then
       Dim year = service("year").InnerText
    End If
Next

就个人而言,我会使用LINQ to XML来解析文件(但这可能是因为我对LINQ的所有事情都很着迷!)。结合VB.NET对XML Literals的支持,它使得一些非常漂亮的代码(恕我直言)。

以下是您可以粘贴到LINQPad的完整示例。

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

这为您提供以下内容:

LINQPad Results

答案 1 :(得分:0)

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

xelement = XElement.Load(file);
services = xelement.Element("services").Elements("service");
相关问题