Results 1 to 5 of 5

Thread: Reading XML file

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Question Reading XML file

    Hello, how would I read an xml file that is structured like this:

    <Books>
    <BookTitle>Book title
    <Author>Author's name</Author>
    <Notes>Personal Notes</Notes>
    </BookTitle>

    <BookTitle>Book title 2
    <Author>Author's name 2</Author>
    <Notes>Personal Notes 2</Notes>
    </BookTitle>
    </Books>

    It just keeps going.

    Lets say that I have a ComboBox with autocomplete (the book titles as the source), how would I, after selecting a book, read each node to a textbox (3 textboxes in total)? Just the part in between <BookTitle></BookTitle>

    Thanks!
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  2. #2
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: Reading XML file

    You could use serialization to read the XML file. Create a few classes like this:
    Code:
    <Xml.Serialization.XmlType("BookTitle")> _
    Public Class Book
    
        Private _Author As String
        <Xml.Serialization.XmlElement("Author")> _
        Public Property Author() As String
            Get
                Return _Author
            End Get
            Set(ByVal value As String)
                _Author = value
            End Set
        End Property
    
    
        Private _Notes As String
        <Xml.Serialization.XmlElement("Notes")> _
        Public Property Notes() As String
            Get
                Return _Notes
            End Get
            Set(ByVal value As String)
                _Notes = value
            End Set
        End Property
    
    
        Private _Title As String
        <Xml.Serialization.XmlText()> _
        Public Property Title() As String
            Get
                Return _Title
            End Get
            Set(ByVal value As String)
                _Title = value
            End Set
        End Property
    
    
    
    End Class
    
    Public Class Books
    
        Private _ListOfBooks As New List(Of Book)
        <Xml.Serialization.XmlElement("BookTitle")> _
        Public Property ListOfBooks() As List(Of Book)
            Get
                Return _ListOfBooks
            End Get
            Set(ByVal value As List(Of Book))
                _ListOfBooks = value
            End Set
        End Property
    
    
    End Class
    Then you can load it from your form like this:

    Code:
            Dim data As Books
            Dim sr As New IO.StreamReader("C:\temp\xmltest.xml")
            Dim x As New Xml.Serialization.XmlSerializer(GetType(Books))
    
            data = DirectCast(x.Deserialize(sr), Books)
            sr.Close()
    
            For Each b As Book In data.ListOfBooks
                MessageBox.Show(String.Format("{0} - {2},{1}", b.Title.Trim(), b.Author, b.Notes))
            Next
    So now we have all of our books in that data object and you can then access it as needed from other parts of the application. You would probably just implement a find method based on the book title and then get back the Book object which will give you access to the Author and notes.

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Reading XML file

    if you're using vb2008, you could use linq:

    vb Code:
    1. Dim books As New Xml.Linq.XDocument(Xml.Linq.XDocument.Parse(IO.File.ReadAllText("books.xml")))
    2.  
    3. Dim DataSource = (From items In books...<BookTitle> Select New With _
    4.                                  { _
    5.                                     .title = items.FirstNode.ToString, _
    6.                                     .author = items.<Author>.Value, _
    7.                                     .notes = items.<Notes>.Value, _
    8.                                     .show = .title & Environment.NewLine & _
    9.                                     .author & Environment.NewLine & _
    10.                                     .notes}).ToList
    11.  
    12. ComboBox1.DataSource = DataSource
    13. ComboBox1.DisplayMember = "title"
    14. ComboBox1.ValueMember = "show"
    15.  
    16. ComboBox1.SelectedIndex = -1
    17. Application.DoEvents()
    18. ComboBox1.SelectedIndex = 0

    vb Code:
    1. Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
    2.     If ComboBox1.SelectedIndex <> -1 Then
    3.         TextBox1.Text = ComboBox1.SelectedValue.ToString
    4.     Else
    5.         TextBox1.Text = ""
    6.     End If
    7. End Sub

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: Reading XML file

    So, by following your example .paul. all the information would be stored in a list? And the items of the list would be in the same order as in the xml file?
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Reading XML file

    it would be stored in a list which would be used as the datasource for the combobox. the items would be in the same order as the xml unless you chose to sort them differently.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width