vb.net读取多层xml文件代码

  Public Sub GetModuleDataFromXml()
        Dim reader As New XmlTextReader(DataFilename)
        'move to the strat of the document
        reader.MoveToContent()
        'start working through the document

        Dim addressData As Collection = Nothing
        Dim elementName As String = Nothing
        Try
            Do While reader.Read
                'what kind of node to we have?
                Select Case reader.NodeType
                    'is it the start of an element
                    Case XmlNodeType.Element
                        'if it's an element start,is it "Address"
                        If reader.Name = "Car" Then
                            'if so,create a new collection..
                            addressData = New Collection()
                        Else
                            'if not ,record the name of the collection
                            elementName = reader.Name

                        End If
                        'if we have some text,try storing it in the collection...
                    Case XmlNodeType.Text
                        'do we have an address?
                        If Not addressData Is Nothing Then
                            addressData.Add(reader.Value, elementName)
                        End If
                    Case XmlNodeType.EndElement
                        'if it is, we should have an entire address stored...
                        If reader.Name = "Car" Then

                            Try
                                With Me
                                    .Label1.Text = addressData("label1")
                                    .Label2.Text = addressData("label2")
                                    .Label3.Text = addressData("label3")
                                    .Label4.Text = addressData("label4")
                                    .Label5.Text = addressData("label5")

                                End With
                                'With carTest

                                '    .strCarStyle = addressData("firstname")
                                '    .strEngine = addressData("lastname")
                                '    .intCareer = addressData("address1")
                                '    .strRefrigerationStyle = addressData("address2")
                                '    .intCylinderNum = addressData("region")

                                'End With
                                'carTest()
                                'carTest()
                                'item = addressData("firstname") & " " & addressData("lastname")
                                'item &= "(" & addressData("email") & ")"
                            Catch ex As Exception
                            End Try

                            'reset...
                            addressData = Nothing
                        End If
                End Select
            Loop
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            If (reader IsNot Nothing) Then
                reader.Close()
            End If

        End Try


    End Sub

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值