?
Solved

VB2005 Express XML style files

Posted on 2006-03-29
5
Medium Priority
?
274 Views
Last Modified: 2012-05-05
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
0
Comment
Question by:hotkeys
  • 3
  • 2
5 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 16322010
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
 

Author Comment

by:hotkeys
ID: 16322176
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
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 16322222
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
 

Author Comment

by:hotkeys
ID: 16322252
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
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 750 total points
ID: 16322329
In that case you are probably stuck with parsing the lines manually using IndexOf, Substring, etc.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month14 days, 1 hour left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question