Jeff
Jeff

Reputation:

Writing XML with VB 6.0

I need to write an error log to a XML file using Visual Basic 6.0. Is there an easy way to do this?

The error log will contain the error number, source, desc, and a timestamp.

Thanks, Jeff

Upvotes: 0

Views: 1495

Answers (3)

gimpf
gimpf

Reputation: 4511

If you are writing the log-file only, I'd recommend just concatenating a string like this:

Public Function GetXmlEntry(errNumber as Long, description as String) as String
  GetXmlEntry = "<entry><error-number>" & errNumber & "</error-number><description>" & description & "</description></entry>"
End Function

Using the XML libraries is not necessary here, and will even hurt performance unnecessarily (i.e. without an advantage).

(edit) As it was pointed out in the comments, writing proper XML requires at least two additional steps for even one single entry: First, one needs to properly escape some special characters (should not be that hard); second, one needs to use correct (=same) encoding for all the XML file, consistent with the declaration in the header (for this my VB-Fu is not sufficient -- a little digging in the reference will help).

Upvotes: 1

Binary Worrier
Binary Worrier

Reputation: 51719

The Microsoft MSXML libraries are your first port of call, but there are things you need to consider before you decide to use it.

Appending error messages to an Xml using the MSML library will parse and load the Xml file every time you open it. As the Xml file grows you will find your app slows to a crawl.

It would be good if you could "just" append your error to the end of the file, however because of closing tags, this isn't straightforword, but it can be done.

If you have control over how the Xml file is created, you can work around this performance bottle next by splitting the Xml file into two, one with the header and one with the body of the xml. Then you can append to the end of the body file (by using an Xml writer class, or by simply appending text to the file).

See Efficient Techniques for Modifying Large XML Files for more info

However all this is unnecessarry if you're writing one error to the xml file, and the next error goes into another Xml file. In that case, go with MSXML

Hope this helps.

Upvotes: 2

Dirk Vollmar
Dirk Vollmar

Reputation: 176239

Probably it's best to use Microsoft's MSXML libraries to do that.

There is a sample in the knowledge base: http://support.microsoft.com/kb/286817

Upvotes: 4

Related Questions