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

来源:互联网 发布:加工中心钻孔循环编程 编辑:程序博客网 时间:2024/04/26 23:53

  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

原创粉丝点击