Private Sub ExecuteXMLButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExecuteXMLButton.Click ' Create a StringBuilder to store the results of the query Dim myItem As String = String.Empty 'results As New System.Text.StringBuilder ' Create an instance of the Command object Dim XMLCommand As New SqlCommand With XMLCommand .Connection = NWConn .CommandType = CommandType.Text .CommandText = "SELECT CompanyName FROM Customers For XML Auto" .Connection.Open() End With Try ' Assign the results of the SQL statement to a data reader Dim reader As System.Xml.XmlReader = XMLCommand.ExecuteXmlReader While reader.Read myItem = reader("CompanyName").ToString lbXML.Items.Add(myItem) End While ' Close the data reader and the connection reader.Close() XMLCommand.Connection.Close() Catch ex As Exception MessageBox.Show("An error occurred while creating your XML list. " & ex.Message, _ "System Message", MessageBoxButtons.OK, MessageBoxIcon.Error) End Try '---now save results to XML file If SaveToXMLFile() Then MessageBox.Show("Your data has been saved as XML.", "System Message", _ MessageBoxButtons.OK, MessageBoxIcon.Information) Else MessageBox.Show("An error occurred while saving to XML.", "System Message", MessageBoxButtons.OK, MessageBoxIcon.Error) End If End Sub Friend Shared Function SaveToXMLFile() As Boolean '---xmlWriter Settings Dim settings As New XmlWriterSettings settings.Indent = True settings.IndentChars = Space(4) 'just for clarity Try '---create writer Dim myWriter As XmlWriter = XmlWriter.Create(My.Settings.CustIDFile, settings) '---create the header myWriter.WriteStartDocument() myWriter.WriteStartElement("Customers") '---now write each element to the file from each listbox item For Each myItem As String In My.Forms.Form1.lbXML.Items myWriter.WriteStartElement("CompanyName") myWriter.WriteElementString("Description", myItem.ToString.Trim) myWriter.WriteEndElement() Next '---all done myWriter.Close() Return True Catch ex As Exception Return False End Try End Function