How To Create Xml File In Vb6
-
Jun 1st, 2008,12:30 PM #1
Thread Starter
Junior Member
VB6 write an XML file
Hi there,
I've to log some program error in a xml file.. i'm just confused about this ..
What i've done is this: every time an error occured i do ..
Code:
strLog = strLog & "<Log>" & vbCrLf strLog = strLog & "<DataError='" & FormatDateTime(Now(), 2) & "'>" & vbCrLf strLog = strLog & "<ErorrCode='" & ErorrCode & "'>" & vbCrLf strLog = strLog & "</Log>" & vbCrLf Open App.Path & "\Log.xml" For Binary As #1 Put #1, 1, strLog Close #1
is this the right way to do that ?
if yes, how could i do to let new error doesn't previous error log ?..as you can see ..i'm not so good with files .. especially xml
-
Jun 1st, 2008,03:08 PM #2
Junior Member
Re: VB6 write an XML file
You do have a typo
You've spelt Error as Erorr
If it's not that - what is the error code your getting
-
Jun 1st, 2008,05:52 PM #3
Thread Starter
Junior Member
Re: VB6 write an XML file
sorry..
I just want to log an error that my vb6 application has in some situation.when an error occurred, i just want to log it in an xml file.
i want to log the time and the error code.but i don't know how could be the right way to write an xml with this information. i don't know much about xml file..
-
Jun 1st, 2008,06:02 PM #4
Junior Member
Re: VB6 write an XML file
Ah! I see what you are saying now.
XML is a big subject in it's own right. Why XML may I ask, is it critical or could you write to a db?
I'm no expert on XML but your tagging looks suspect (no closing DataError or ErrorCode tags/slash)
How do you intend to read/parse the XML to extract the error data?
Dave
-
Jun 1st, 2008,06:09 PM #5
Thread Starter
Junior Member
Re: VB6 write an XML file
I'm sure .. i'm in wrong but i tought that making a log in xml could be easyer to see from a direct webpage address like www.mydomain.com/log.xml
and over i prefer to write a log in a file and not in a db just to let bd work without other external stress..
but if this is a wong way.. i'll change it.
-
Jun 1st, 2008,07:03 PM #6
Re: VB6 write an XML file
You could do this but it makes for some extra headaches.
To start with you aren't creating XML there. An XML node gets its values from attributes or subelements (or both):
Code:
<Log DateError='1/1/2007 @ 12:27:32' ErrorCode='4712' />
Code:
<Log> <DateError>1/1/2007 @ 12:27:32</DateError> <ErrorCode>4712</ErrorCode> </Log>
Code:
<Log DateError='1/1/2007 @ 12:27:32'> <ErrorCode>4712</ErrorCode> </Log>
The first is that to do much with this via standard software you really need it to be well formed. At a minimum this means you need a document tag and close tag as well as tags for record elements and field elements:
Code:
<Log> <Entry DateError='1/1/2007 @ 12:27:32'> <ErrorCode>4712</ErrorCode> </Entry> <Entry DateError='1/1/2007 @ 12:35:07'> <ErrorCode>318</ErrorCode> </Entry> <Entry DateError='1/1/2007 @ 14:09:47'> <ErrorCode>56423</ErrorCode> </Entry> </Log>
The second thing is that you'll need to do entity encoding for a long list of special characters that have meaning to XML:
Code:
<Log> <Entry DateError='1/1/2007 & 12:27:32'> <ErrorCode>4712</ErrorCode> </Entry> <Entry DateError='1/1/2007 & 12:35:07'> <ErrorCode>318</ErrorCode> </Entry> <Entry DateError='1/1/2007 & 14:09:47'> <ErrorCode>56423</ErrorCode> </Entry> </Log>
But we're not quite done. You're really supposed to have an XML declaration prefix heading the document itself. Example for a case where the file is written as typical Windows ANSI text:
Code:
<?xml version="1.0" encoding="ISO-8859-1"?> <Log> <Entry DateError='1/1/2007 & 12:27:32'> <ErrorCode>4712</ErrorCode> </Entry> <Entry DateError='1/1/2007 & 12:35:07'> <ErrorCode>318</ErrorCode> </Entry> <Entry DateError='1/1/2007 & 14:09:47'> <ErrorCode>56423</ErrorCode> </Entry> </Log>
To top it all off the standard convention is to terminate lines in an XML document with LF, not CRLF. Thankfully most Windows software for handling XML will accept CRLF line ends without complaint. Just don't send it to a Mac or Unix/Linux machine!
What's so bad about a CSV or Tab-delimited file again?
-
Jun 1st, 2008,08:37 PM #7
Junior Member
Re: VB6 write an XML file
If you want the log to be visible from the web, why use XML? You could use plain old HTML - creating your file will be that much simpler.
-
Jun 1st, 2008,08:54 PM #8
Re: VB6 write an XML file
An HTML log file has the same problem of being "poorly formed" unless you write the tags to close out the document and back over them every time you append. The good news is that web browsers are more tolerant of this than XML parsers are, so in theory you can leave the HTML log with hanging unclosed tags (i.e. no </table>, no </body>, and no </html>).
My guess is the sample link above was showing XML data after processing by an XSLT or something.
-
Jun 2nd, 2008,07:59 AM #9
Re: VB6 write an XML file
Here is an example of how you can do it using the XML DOM
Code:
'Set a reference to "Microsoft XML, v3.0" Option Explicit Private Sub Command1_Click() Dim i As Integer Randomize i = Int(Rnd() * 5000) + 1 LogError Now, i End Sub Private Sub LogError(ByVal ErrDate As Date, ByVal ErrCode As Long) Dim objDoc As MSXML2.DOMDocument Dim xmlProcessingInstruction As MSXML2.IXMLDOMProcessingInstruction Dim objRoot As IXMLDOMNode Dim objNode As IXMLDOMNode Dim objNodeErr As IXMLDOMNode Dim objNodeDetails As IXMLDOMNode Dim strLogPath As String strLogPath = "c:\ErrorLog.xml" 'Create an XML Document object Set objDoc = New MSXML2.DOMDocument 'Check if the log exists. If it does open it otherwise create it If Dir(strLogPath) = "" Then 'Creating standard headers Set xmlProcessingInstruction = objDoc.createProcessingInstruction("xml", "version='1.0' encoding='UTF-8'") objDoc.appendChild xmlProcessingInstruction Set xmlProcessingInstruction = Nothing 'Create the Root Node Set objRoot = objDoc.createElement("log") objDoc.appendChild objRoot Set objRoot = Nothing Else objDoc.Load strLogPath End If 'Get the Root Node Set objRoot = objDoc.selectSingleNode("log") 'Add an error node to the root node Set objNodeErr = objDoc.createElement("error") objRoot.appendChild objNodeErr Set objRoot = Nothing 'Add the details to the error node Set objNodeDetails = objDoc.createElement("DateError") objNodeDetails.Text = FormatDateTime(ErrDate, 2) objNodeErr.appendChild objNodeDetails Set objNodeDetails = objDoc.createElement("ErrorCode") objNodeDetails.Text = ErrCode objNodeErr.appendChild objNodeDetails 'clean up Set objNodeDetails = Nothing Set objNodeErr = Nothing Set objNode = Nothing 'Save the log objDoc.save strLogPath Set objDoc = Nothing End Sub
-
Jun 2nd, 2008,08:22 AM #10
Re: VB6 write an XML file
Yes but nobody uses MSXML for logging. The overhead is stupendous. Anything with a DOM will kill you on both cycles and memory use.
You can create lightweight XML generators or just write out log records as the OP was hinting at. You just have to do that "backing over" thing with anything at the tail of the document.
-
Jun 2nd, 2008,08:10 PM #11
Re: VB6 write an XML file
Originally Posted by cuollins
Hi there,
I've to log some program error in a xml file.. i'm just confused about this ..
What i've done is this: every time an error occured i do ..
Code:
strLog = strLog & "<Log>" & vbCrLf strLog = strLog & "<DataError='" & FormatDateTime(Now(), 2) & "'>" & vbCrLf strLog = strLog & "<ErorrCode='" & ErorrCode & "'>" & vbCrLf strLog = strLog & "</Log>" & vbCrLf Open App.Path & "\Log.xml" For Binary As #1 Put #1, 1, strLog Close #1
Cuold be this ..an xml file ?
is this the right way to do that ?
if yes, how could i do to let new error doesn't previous error log ?..as you can see ..i'm not so good with files .. especially xml
-
Jun 2nd, 2008,08:26 PM #12
Junior Member
Re: VB6 write an XML file
I still think for this particular purpose HTML is the way to go. To keep it well formed would be a piece of cake as there need only be a couple of closing tags and these can be stored in a variable.
When a new error is to be logged, the app reads in the existing file, strips out the end tags, appends the new error and rewrites the closing tags.
Minimum code for the result required - XML for this is surely overkill?
Dave
-
Jun 3rd, 2008,05:14 AM #13
Thread Starter
Junior Member
Re: VB6 write an XML file
hey guys thank you all,
the vb code works fine, is what i was looking for, now i know ho to manage an xml : )
..
maybe xml could be better then html just because i can execute some kind of filter - query to get the data i need... for examples.. how many errors in a specifc time, this could be more difficult with a simple html..
but i could be in wrong..I prefered to avoid mssql because it has a lot of work to do and prefered to avoid access.. because more then once it appens thet the file was corrupted.
How To Create Xml File In Vb6
Source: https://www.vbforums.com/showthread.php?525218-VB6-write-an-XML-file
Posted by: hatchhadmingesen.blogspot.com
0 Response to "How To Create Xml File In Vb6"
Post a Comment