locked
How to convert XML file into List(of Object) in vb.net ?

    Question

  • Hi,

     

    Please tell me how to convert XML file into List(of Object) in vb.net.

     

    Regards,

    JN

     

    Monday, June 20, 2011 11:14 AM

Answers

  • Hi JN,

    Welcome to the MSDN Forum.

    Please take a look at this code snippets:

    'Read the XML file subroutine
      Public Sub readHouse(xmlFilePath As String)
        Dim dokument As XmlReader = New XmlTextReader(xmlFilePath)
        Dim objHouseList As New List(Of objHouse)
        dokument.Read()
        While dokument.NodeType <> XmlNodeType.None
          Dim type = dokument.NodeType
          If (type = XmlNodeType.Element And dokument.Name = "house") Then
            Dim objH As New objHouse
            Dim rooms As New List(Of String)
            While Not (dokument.NodeType = XmlNodeType.EndElement And dokument.Name = "house")
              dokument.Read()
              If (dokument.Name = "id") Then
                Dim id As String = dokument.ReadElementContentAsString()
                Console.WriteLine(id)
                objH.p_id = id
              End If
    
              If (dokument.Name = "rooms") Then
                Dim room As String = dokument.ReadElementContentAsString()
                Console.WriteLine(room)
                rooms.Add(room)
              End If
            End While
            objH.p_rooms = rooms
            objHouseList.Add(objH)
          Else
            dokument.Read()
          End If
        End While
      End Sub
    

    Here is the Object Class:

    Public Class objHouse
      Private id As Integer
      Private rooms As List(Of String)
    
      Property p_id() As Integer
        Get
          Return id
        End Get
        Set(ByVal Value As Integer)
          id = Value
        End Set
      End Property
    
      Property p_rooms() As List(Of String)
        Get
          Return rooms
        End Get
        Set(ByVal Value As List(Of String))
          rooms = Value
        End Set
      End Property
    End Class
    

    Here is the test XML file:

    <?xml version="1.0" standalone="yes"?>
    <root>
    <house>
     <id>1</id>
     <rooms>Living</rooms>
     <rooms>Study</rooms>
     <rooms>Master</rooms>
    </house>
    <house>
     <id>2</id>
     <rooms>Living2</rooms>
     <rooms>Study2</rooms>
     <rooms>Master2</rooms>
    </house>
    </root>
    

    I hope this will be helpful.

    Best regards,


    Mike Feng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, June 22, 2011 9:52 AM

All replies

  • Unless You want Your own wrapper - System.Xml.Linq - You can use it to parse XML, and as I recall it has IEnumerable/ICollection interface

    Cheers,
    Sami 


    Nimi projects | Unconventional IT development since 8th August 2008
    Monday, June 20, 2011 9:29 PM
  • Hi JN,

    Welcome to the MSDN Forum.

    Please take a look at this code snippets:

    'Read the XML file subroutine
      Public Sub readHouse(xmlFilePath As String)
        Dim dokument As XmlReader = New XmlTextReader(xmlFilePath)
        Dim objHouseList As New List(Of objHouse)
        dokument.Read()
        While dokument.NodeType <> XmlNodeType.None
          Dim type = dokument.NodeType
          If (type = XmlNodeType.Element And dokument.Name = "house") Then
            Dim objH As New objHouse
            Dim rooms As New List(Of String)
            While Not (dokument.NodeType = XmlNodeType.EndElement And dokument.Name = "house")
              dokument.Read()
              If (dokument.Name = "id") Then
                Dim id As String = dokument.ReadElementContentAsString()
                Console.WriteLine(id)
                objH.p_id = id
              End If
    
              If (dokument.Name = "rooms") Then
                Dim room As String = dokument.ReadElementContentAsString()
                Console.WriteLine(room)
                rooms.Add(room)
              End If
            End While
            objH.p_rooms = rooms
            objHouseList.Add(objH)
          Else
            dokument.Read()
          End If
        End While
      End Sub
    

    Here is the Object Class:

    Public Class objHouse
      Private id As Integer
      Private rooms As List(Of String)
    
      Property p_id() As Integer
        Get
          Return id
        End Get
        Set(ByVal Value As Integer)
          id = Value
        End Set
      End Property
    
      Property p_rooms() As List(Of String)
        Get
          Return rooms
        End Get
        Set(ByVal Value As List(Of String))
          rooms = Value
        End Set
      End Property
    End Class
    

    Here is the test XML file:

    <?xml version="1.0" standalone="yes"?>
    <root>
    <house>
     <id>1</id>
     <rooms>Living</rooms>
     <rooms>Study</rooms>
     <rooms>Master</rooms>
    </house>
    <house>
     <id>2</id>
     <rooms>Living2</rooms>
     <rooms>Study2</rooms>
     <rooms>Master2</rooms>
    </house>
    </root>
    

    I hope this will be helpful.

    Best regards,


    Mike Feng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, June 22, 2011 9:52 AM