Help with writing error information to an XML file


I am new to VB.NET/XML so please bear with me. I am trying to write a method called ErrorMethod() which will take an exception object and write the error information to an XML file. If the file already exists the error data should be appended to the XML file. This is what the XML file SHOULD look like:
<?xml version="1.0" ?>
<!-- Comments: WebServices Error Log  -->
      <Date> 31/05/2006</Date>
      <Time> 02:50:48</Time>
      <Date>31/05/2006 </Date>
      <Time>12:50:48 </Time>

Each Error node contains the information about each error. Now this is what I am getting when I run the code and look at the XML file in notepad:

<?xml version="1.0"?>
<!--Comments: WebServices Error Log-->

Could some please look at the code below and suggest what improvements could be made to make it look like it should. Thanks!!


And now for the code:

    Private Sub ErrorMethod(ByVal ex As Exception)
        ' Do something here to write error message to error log        
        ' Write Out : Date,Time,User,WebServiceName,MethodName,LineNo,ErrNo,Message
        Dim sErrFile As String = ConfigurationManager.AppSettings("ErrorLogFileName").ToString
        Dim sError As String
        Dim dToday As DateTime = System.DateTime.Now        

        sError = ""
        ' File doesn't exist so create it
        If Not File.Exists(sErrFile) Then
            Dim xmlWriter As New XmlTextWriter(sErrFile, Nothing)
            With xmlWriter
                .WriteComment("Comments: WebServices Error Log")
            End With
        End If

        ' Append Error node to end of Errors node
        Dim doc As New XmlDocument()
        Dim node As XmlDocumentFragment        
        Dim sXML As String = ""

        'build document...        
        sXML = String.Concat(sXML, ControlChars.CrLf, "<Error>")
        sXML = String.Concat(sXML, ControlChars.CrLf, ControlChars.Tab, "<Date>")
        sXML = String.Concat(sXML, ControlChars.CrLf, ControlChars.Tab, dToday.ToString("dd/MM/yyyy"))
        sXML = String.Concat(sXML, ControlChars.CrLf, ControlChars.Tab, "</Date>")
        sXML = String.Concat(sXML, ControlChars.CrLf, ControlChars.Tab, "<Time>")
        sXML = String.Concat(sXML, ControlChars.CrLf, ControlChars.Tab, dToday.ToString("hh:mm:ss"))
        sXML = String.Concat(sXML, ControlChars.CrLf, ControlChars.Tab, "</Time>")        
        sXML = String.Concat(sXML, ControlChars.CrLf, "</Error>")

        doc.Load(sErrFile)                              ' Load existing error log
        node = doc.CreateDocumentFragment()             ' Create document fragment from original XML doc
        node.InnerText = sXML                           ' Associate XML string with document fragment

        doc.DocumentElement.AppendChild(node)           ' Append document fragment to main document        
        doc.Save(sErrFile)                              ' Save XML document back to file
    End Sub
Who is Participating?
Bob LearnedConnect With a Mentor Commented:
Here is a very good XML tutorial:

Bob LearnedCommented:
1) XML suggestion:

     <Error time="30/05/2006 02:50:48" />
     <Error time="31/05/2006 02:50:48" />
     <Error time="31/05/2006 12:50:48" />

  This will keep the file structure more compact and readable.

2) Are you going to limit the number of error written, or can the file size just keep growing to MB in size?

3) Plain text strings get HTML encoded, which is why '<' is written as &lt.

chriscboyAuthor Commented:
1) That might not work in my case. I do intend on adding additional elements to the error node. I am just trying to initially get it working with Date and Time elements but would also like to add elements for ErrorNumber,ErrorDescription,ErrorMethod etc.

2) No, this file will grow and grow (wouldn't it be nice if I got no errors!?!?)

3) Ah, that makes some sense, because that is what I saw when I opened the XML file in notepad. If I open in IE6, I get the following :
<?xml version="1.0" ?>
<!-- Comments: WebServices Error Log-->

The entire <Error>...</Error> appears in black as well.

Which would indicate I am doing something wrong in my code?
Bob LearnedCommented:
If you write CDATA elements, they don't get parsed/encoded.

Here is an example:

function matchwo(a,b)
if (a < b && a < 0) then
   return 1
   return 0

chriscboyAuthor Commented:
Sorry that did not make any sense! I'm completely new to XML so don't even know what CDATA means!

I am trying to write out a simple XML file with the tags previously mentioned. I think I will write the error info out to a simple CSV file, as I know what to do there. XML can wait until I understand it a bit more. Thanks for trying.
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.