tags:

views:

292

answers:

3

Hello!

i have a log file which contains hundreds/thousands of seperate XML messages and need to find a way to extract a complete xml message depending on the parameters given (values of nodes).

My biggest problem is that even though i program a fair amount i have had very little contact with XML or the XML libraries of the languages i use and didnt think that a simple text parsing would be an elegant solution!

I am going to attempt this in C# or VB.net any help would be much appreciated and any attempt at a solution even better!

thanks in advance!

+3  A: 

EDIT: The answer below assumes that the whole log file is a valid XML document. Ignore if that's not the case.

XPath is probably your answer here - assuming you can afford to load the whole log file in one go, it should be fairly easy to use XmlNode.SelectSingleNode or XNode.XPathSelectElement.

Alternatively, if you're using LINQ to XML you could construct a LINQ query - that may well be more readable if you're not familiar with XPath.

If you want to learn XPath, I've usually found the W3Schools tutorial pretty good.

Jon Skeet
A: 

A very basic approach would be to:

  1. Parse the file. You can extract each Xml message and treat it as one complete document.
  2. Based on the parameters query the xml and determine if it's a match.

This solution is not the greatest, I would wager it will perform poorly as you'll be loading quite a lot, although only testing would know. This approach has a benefit in that the file doesn't have to be valid Xml. So if your parsing a file being written to, you'd be able to use this method. (I'm assuming your logger is just appending xml to the file and not treating this as one large dom).

If the file is not being written too, and depending on the size you could wrap the contents of the file in an Xml node. Again this assumes the only thing written to the file is Xml. If your logging additional info then you'll have to go with the first solution.

As for parsing the Xml you have various options such as Linq to Xml, or XPath.

JoshBerke
A: 

For LINQ to XML, something similar to this:

Dim xml As XDocument = XDocument.Load("Messages.xml")
Dim messages = From msg In xml.<Messages>.<Message> Select msg

For Each m In messages
  Console.WriteLine(m.Value.ToString)
Next

Of course, this will have to modified to fit the structure of your XML.

aphoria