Replace & with & in Dir To XML

tonycrew

Well-known member
Joined
Apr 20, 2009
Messages
55
Programming Experience
Beginner
Ok i have a function that lists a dir's contents and creates an XML file in a certain format, but if it comes across a file name that has & in it the xml is not 100% valid so i need to replace all instances of & with &
Anyone got any ideas.

VB.NET:
 Private Sub DirXml_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DirXml.Tick
        If ListBox1.Items.Count = 0 Then
            TextBox13.Text = "No Files"
            DirXml.Enabled = False
            SaveFileDialog2.ShowDialog()
        Else
            ListBox1.SelectedIndex = 0
            TextBox13.Text = ListBox1.SelectedItem.ToString
            For i As Integer = 0 To Me.ListBox1.Items.Count - 1
                Dim fi As New IO.FileInfo(Me.ListBox1.Items(i))
                Me.ListBox1.Items(i) = Me.ListBox1.Items(i).ToString.Replace(fi.Directory.ToString & "\", "")
            Next
            If TextBox14.Text.Contains("</menu>") Then
                TextBox14.Text = TextBox14.Text.Remove(TextBox14.Text.IndexOf("</menu>"))
                TextBox14.AppendText(Environment.NewLine & "	<game name=" & TextBox9.Text & ListBox1.SelectedItem.ToString & TextBox9.Text & ">")
                TextBox14.AppendText(Environment.NewLine & "		<description></description>")
                TextBox14.AppendText(Environment.NewLine & "		<cloneof></cloneof>")
                TextBox14.AppendText(Environment.NewLine & "		<crc></crc>")
                TextBox14.AppendText(Environment.NewLine & "		<manufacturer></manufacturer>")
                TextBox14.AppendText(Environment.NewLine & "		<year></year>")
                TextBox14.AppendText(Environment.NewLine & "		<genre></genre>")
                TextBox14.AppendText(Environment.NewLine & "	</game>")
                TextBox14.AppendText(Environment.NewLine & "</menu>")
            Else
                TextBox14.AppendText(Environment.NewLine & "	<game name=" & TextBox9.Text & ListBox1.SelectedItem.ToString & TextBox9.Text & ">")
                TextBox14.AppendText(Environment.NewLine & "		<description></description>")
                TextBox14.AppendText(Environment.NewLine & "		<cloneof></cloneof>")
                TextBox14.AppendText(Environment.NewLine & "		<crc></crc>")
                TextBox14.AppendText(Environment.NewLine & "		<manufacturer></manufacturer>")
                TextBox14.AppendText(Environment.NewLine & "		<year></year>")
                TextBox14.AppendText(Environment.NewLine & "		<genre></genre>")
                TextBox14.AppendText(Environment.NewLine & "	</game>")
                TextBox14.AppendText(Environment.NewLine & "</menu>")
            End If
            For i As Integer = 0 To Me.ListBox1.Items.Count - 1
                Dim fi As New IO.FileInfo(Me.ListBox1.Items(i))
                Me.ListBox1.Items(i) = Me.ListBox1.Items(i).ToString.Replace(fi.Directory.ToString & "\", "")
            Next
            ListBox1.Items.Remove(ListBox1.SelectedItem)
        End If
    End Sub
 
Writing XML like that is a bit dodgy. You should really be using an XmlWriter to do it. I'm guessing that an XmlWriter would handle things like ampersands and other special characters.

Failing that, you can simply use the String.Replace method.
 
Back
Top