VB.NET (Listboxes & XML)
Go to solution
Solved by Mr_KoKa,
If ListBox1.Items.Count <> ListBox2.Items.Count Then
MessageBox.Show("Listboxes items count does not match")
Return
End If
Dim xmlWriterSettings As XmlWriterSettings = New XmlWriterSettings()
xmlWriterSettings.Indent = True
Dim xmlWriter As XmlWriter = xmlWriter.Create("tv.xml", xmlWriterSettings)
xmlWriter.WriteStartDocument()
xmlWriter.WriteStartElement("TVShowsN")
For index As Integer = 0 To ListBox1.Items.Count - 1
xmlWriter.WriteStartElement("TVShows")
xmlWriter.WriteAttributeString("ID", ListBox1.Items(index))
xmlWriter.WriteAttributeString("Name", ListBox2.Items(index))
xmlWriter.WriteEndElement()
Next
xmlWriter.WriteEndElement()
xmlWriter.WriteEndDocument()
xmlWriter.Flush()
xmlWriter.Close()
The key here is to check if count match, cause if it doesn't you will be try to access items on indexes that are out of bounds for one of listboxes.
If you would let them not match, you would need to check if current loop index is not greater or equal listbox's count, and if it is, omit attribute or leave it empty. Other wise it would probably throw exception.

Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now