Help with writing error information to an XML file

Posted on 2006-05-30
Last Modified: 2010-04-23

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
Question by:chriscboy
    LVL 96

    Expert Comment

    by:Bob Learned
    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.


    Author Comment

    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?
    LVL 96

    Expert Comment

    by:Bob Learned
    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


    Author Comment

    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.
    LVL 96

    Accepted Solution

    Here is a very good XML tutorial:


    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Introduction When many people think of the WebBrowser ( control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    728 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

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now