|
-
Dec 7th, 2009, 08:41 PM
#1
Thread Starter
Fanatic Member
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!
-
Dec 7th, 2009, 09:46 PM
#2
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.
-
Dec 8th, 2009, 03:06 AM
#3
Re: Reading XML file
if you're using vb2008, you could use linq:
vb Code:
Dim books As New Xml.Linq.XDocument(Xml.Linq.XDocument.Parse(IO.File.ReadAllText("books.xml")))
Dim DataSource = (From items In books...<BookTitle> Select New With _
{ _
.title = items.FirstNode.ToString, _
.author = items.<Author>.Value, _
.notes = items.<Notes>.Value, _
.show = .title & Environment.NewLine & _
.author & Environment.NewLine & _
.notes}).ToList
ComboBox1.DataSource = DataSource
ComboBox1.DisplayMember = "title"
ComboBox1.ValueMember = "show"
ComboBox1.SelectedIndex = -1
Application.DoEvents()
ComboBox1.SelectedIndex = 0
vb Code:
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
If ComboBox1.SelectedIndex <> -1 Then
TextBox1.Text = ComboBox1.SelectedValue.ToString
Else
TextBox1.Text = ""
End If
End Sub
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Dec 8th, 2009, 08:33 AM
#4
Thread Starter
Fanatic Member
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?
-
Dec 8th, 2009, 08:36 AM
#5
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.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|