Writing / Appending to an XML file

herculesgomez

New member
Joined
Sep 19, 2006
Messages
1
Programming Experience
Beginner
Can someone please explain to me how to create the below XML file. I want to create an error log
which will capture the data in XML. I want to used a non cached approach and be able to
append to the file if the correct elements and nodes already exist.

Each error will have an error type and an error date.

Ive created a blank XML file within my visual studio project called ErrorLog.xml.
I've been looking all over the web for some good
examples of writing data to an XML document but am unable to find any good ones.

Below is an example of how the layout I would like.
----------------------------------------
HTML:
<?xml version="1.0"?>
<errorlog>
<error>
<errortype></errortype>
<datetime></datetime>
</error>
</errorlog>

----------------------------------------
Regards,
Herc
 
Last edited by a moderator:
There are (at least) four approaches to this:
1) Read/write the Xml file into a Xml.XmlDocument with Load/Save methods, manually create and append new Xml nodes.

2) Use the .Net utility Xsd.exe to generate a schema from the Xml file, then same utility to generate a VB class. Use this class for Xml serialization to/from file. This makes it much easier to handle the objects in code since you only have to create new class instances and add them to array/collection. You may want to modify the generated schema/class because it is quite simple and doesn't name properties etc very well. You can of course create your own class directly for use with serialization without going via the Xsd.

3) Using XmlTextReader/XmlTextWriter could be done, but seems too much work since this appears as output/append oriented.

4) Read/write the Xml file into a Dataset with ReadXml/WriteXml methods, then work with the tables/columns/rows as with other databases.
 
I was just thinking, Xml may not be the best of means for a log file used for appends since the complete Xml document have to be loaded to be able to append nodes. For logging opening a FileStream in Append mode and writing to it with a StreamWriter is a little simpler and gives better performance. You can also convert/generate the xml from the plain text file when you need this format.
 
Back
Top