Jump to content

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.

Good Afternoon Everyone...

 

Trying to do something that should be stupidly simple but I just can not figure it out. 

 

I want an XML file for two lists so the name of a TV Show and an ID number.

 

I can create an XML file no problem and it'll look something like:

 

<?xml version="1.0" encoding="UTF-8"?>

-<TVShowsN>

<TVShows ID="123456" Name="The Vampire Diaries"/>

<TVShows ID="987654" Name="Arrow"/>

</TVShowsN>

And I can happily add as many as I like.. which is great. I can load that file so that ListBox1 index 0 shows the name and Listbox2 index 0 shows the ID. Index 1 shows the next show and ID etc.... 

 

The problem is I want to be able to add more and save them at runtime. So I have another two text boxes to add a new name to listbox1 and a new ID to listbox2.... the problem is saving both listboxes to the XML file... I just can not get it to save it keeps crashing at different sections depending how I try to resolve it. If anyone knows how to save two listboxes to a single XML file that would be great.

 


Thanks

 

Link to comment
https://linustechtips.com/topic/670479-vbnet-listboxes-xml/
Share on other sites

Link to post
Share on other sites

The list boxes are always the same.. or should be. So one has the name and the other has the ID. If you select one show from list box 1 then it also selects the ID in List box 2. There should never be a case where 1 is populated and 2 is empty. They get added together, loaded together and hopefully saved together.

 

So for example above..

listbox1 index 0 = "The Vampire Diarie" listbox2 index 0 = "123456"

listbox1 index 1 = "Arrow" listbox2 index 1 = "987654"

 

Thanks

 

Link to comment
https://linustechtips.com/topic/670479-vbnet-listboxes-xml/#findComment-8644922
Share on other sites

Link to post
Share on other sites

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.

Link to comment
https://linustechtips.com/topic/670479-vbnet-listboxes-xml/#findComment-8644953
Share on other sites

Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

×