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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
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.
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
Carl TawnSystems and Integration DeveloperCommented:
In that case you are probably stuck with parsing the lines manually using IndexOf, Substring, etc.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.