Resolved Problem to read multiple xml files

Joined
Dec 15, 2010
Messages
5
Programming Experience
1-3
I'm new to .net programming and have ran into an issue I can't seem to find an answer for where ever I look. I have a multi-line text box where one would input the name of a computer. I want to read each computer as an array element and go to the corresponding .xml file to pull down data and write it to excel. Here is the basic code I'm using:

Dim arrComputers As String = TextBox1.Text.Split(vbCrLf)
For Each strComputer As String In arrComputers
'XML Parsing
Dim reader As XmlTextReader = New XmlTextReader("C:\XML_Files\" & strComputer & ".xml")
While reader.Read()
'Do something
End While
Next

It works perfect when there is only 1 computer listed. However once I have more than a single computer I get an error telling me I have an illegal character path at "While reader.Read()". Any help would be immensely appreciated. Thanks in advance for any advice you can give.
 
Last edited:
VB.NET:
Dim arrComputers As String = TextBox1.Text.Split(vbCrLf)

arrComputers should be an array:

VB.NET:
Dim arrComputers() As String = TextBox1.Text.Split(vbCrLf)

You should also close your reader when you're done reading from it. A Using statement will do this for you.

VB.NET:
        TextBox1.Text = "Books" & Environment.NewLine & "Books2"
        Dim arrFiles() As String = TextBox1.Text.Split(Environment.NewLine)

        For Each f As String In arrFiles
            Using reader As New Xml.XmlTextReader(String.Format("C:\Temp\{0}.xml", f))
                While reader.Read()
                    'Do Something
                End While
            End Using
        Next
 
There is no Split(String) method (see String.Split Method (System)), so for some reason will you end up with array elements with vbLf prepended to them and that is an illegal char in a file path.
I recommend you use this version: .Split(String(), StringSplitOptions)
(Actually I often use the Regex.Split instead)
 
Thank you both very much for the input. JohnH you were right, once I replaced my code with the following it worked perfectly. Thank you so much.
VB.NET:
Dim arrComputers() As String
        arrComputers = TextBox1.Text.Split(TryCast(Nothing, String()),
                       StringSplitOptions.RemoveEmptyEntries)
 
Back
Top