VB2005 Express XML style files

Hello All (I am a real beginner with this so please bear with me)

I am hoping to use VB2005 Express to make alterations to the text files produced by our document management system. I have had a look at some of the tools in  VB to deal with XML but need some direction - I currently have 1/2 a million of these files to modify :(

The main problem is the text files I am dealing with are not 100% XML, the header structure is:

<FILE_NAME>This is the file name</FILE_NAME>
<BOX_NUMBER>This is the box nuber</BOX_NUMBER>
<Job_Number>This is a job number</Job_Number>
<Job_Name>This is the job name</Job_Name>
<Client_Name>This is the client name</Client_Name>
<Section>This is the section name</Section>
<Sub_Contractors>This is the Sub Contractos name</Sub_Contractors>
<Supply_Only_Orders>This is the suppliers name</Supply_Only_Orders>

The text file then holds the OCD'd tet of the document plus co-ordinate data etc.

What I would like to do is read the information in from the fields above and replace information as required. I could use INSTR and MID to get the info but hope there is a cleaner way.

Thanks

John
hotkeysAsked:
Who is Participating?
 
Carl TawnSystems and Integration DeveloperCommented:
In that case you are probably stuck with parsing the lines manually using IndexOf, Substring, etc.
0
 
Carl TawnSystems and Integration DeveloperCommented:
The simplest thing to do is probably to make the file contents into valid XML, then work with it using an XmlDocument.

Something like:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim fileName As String = "C:\Temp.txt"

        If File.Exists(fileName) Then

            Dim tr As New FileStream(fileName, FileMode.Open)
            Dim sr As New StreamReader(tr)

            Dim content As String = sr.ReadToEnd()

            sr.Dispose()
            tr.Dispose()

            Dim doc As New XmlDocument()
            doc.LoadXml("<root>" & content & "</root>")

            '// We can now use methods of the XmlDocument to retrieve/modify the data

        End If

    End Sub
0
 
hotkeysAuthor Commented:
Thank you for the idea,

I have modified the code slightly, I think generally to work in 2005:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim fileName As String = "z20003a1b.txt"
        If My.Computer.FileSystem.FileExists(fileName) Then

            Dim tr As New IO.FileStream(fileName, System.IO.FileMode.Open)
            Dim sr As New IO.StreamReader(tr)

            Dim content As String = sr.ReadToEnd()

            sr.Dispose()
            tr.Dispose()

            Dim doc As New Xml.XmlDocument()
            doc.LoadXml("<root>" & content & "</root>")
            '// We can now use methods of the XmlDocument to retrieve/modify the data

        End If
    End Sub

However I get an error

A first chance exception of type 'System.Xml.XmlException' occurred in System.Xml.dll

With regard to invalid character in string, ' ', hexadecimal value 0x0C, is an invalid character. Line 11, position 8.

John
0
 
Carl TawnSystems and Integration DeveloperCommented:
That suggests that there is a character in your file that is making the XML invalid. You may have to do some work with the string returned by sr.ReadToEnd() in order to escape or remove them.
0
 
hotkeysAuthor Commented:
Thanks Carl

I can't do that as they need to remain intact for the DMS to work. I only want to make changes to the "Key Field" data in their headers.

John
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.