Strategy to read part of XML log file?



Strategy to read part of XML log file


My log file will contains entries formatted as XML(i use this format since
some time i would like to log entire objects)
I am going to use FileSystemWatcher in order to detect when changes occur
to the file
and now I am facing a problem reading the last added node
I came to the following strategy:

when a new entry will be added i will set a numeric value in it's attribute

<Entry id="100">
<Entry id="101">

Since I know what was the last entry read by my log viewer i can
know excalty what entries should be read

My question is do i have some API which support searching or getting part
of XML document without retriveing it to memory first?

Thanks in advance.

Justin Rogers

I'm not 100% sure you are looking for an XML parser to do this. If you
know newly added items are present at the end, it would be easier to
read the end off of the file using a non XML parser and get the data you
need that way.


Enteries are added to the end of the file,but how can this help me?
i dont know what is the size of the last entry entry
do you mean that i should track the position of the latest entry and use



Ok that seems good as long as I can search anode with specific attribute
I will try it


Justin Rogers

Well, for the XPathReader you can use an expression to grab all
entries without your Id attribute set. This still requires immense
processing of the file. My proposal would be to open the file,
seek from the end (see SeekOrigin), read your buffer, and convert
it to a string, 4K or so would be nice. Then do a LastIndexOf for
your <Entry and you've found your entry... You can continue
reading buffers and prepending the data to your string if the
record is larger than a single read buffer.

Note, I do this sort of thing all the time, being a huge performance
advocate. While the higher level design would call for a pre-canned
class like a full blown XML reader, I tend to circumvent anything
that will require me to read and process large portions of the file.

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question