Click here to Skip to main content
15,794,275 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi all!

My application traces messages into xml-file by using LINQ to XML XElement. Here is sample of result xml-file:

<xml version="1.0" encoding="utf-8"?>
    <timeStamp>10.12.2010 12:05:57</timeStamp>
    <traceMessage>Error happened </traceMessage>
    <timeStamp>10.12.2010 12:06:07</timeStamp>
    <traceMessage>This is just info message</traceMessage>

Now my task is to create WPF application which reads that xml file and shows messages. The problem is that message level varies (error, info.) and I need create some filter or query which shows only wanted messages. For example "show only Error messages". How do I do that?

Or is it even possible query that format or do I have to change little bit my file formation? I kindly ask your help.

I hope you got my idea :)

Updated 30-Dec-10 19:56pm
Hiren solanki 31-Dec-10 2:02am    
see my updated answer, this might help you.
Hiren solanki 31-Dec-10 2:18am    
Thank you raistu for your kind word, Enjoy. Cheers.

1 solution

If your traceMessage Element have always Error word inside then you can filter based on string comparison if it contains Error then it's Error message.

But I would suggest you to add one attribute to Message Element like to show a category whether it's error or Info.


<Message Category="Error">
<Message Category="Info">

It will be easier to apply LINQ to filter data.


Ah!! Thanks for updating raitu, I can suggest you following query that might help you.

var resultXML = from c in TraceMessage.Elements("Message")
                      where ((string)c.Element("traceMessage")).Contains("Error")
                      select new {
                                    Timestamp = (String)c.Element("timestamp"),
                                     Level = (string)c.Element("Level"),
                                     PID = (string)c.Element("PID"),
                                     traceMessage  = (string)c.Element("tracemessage") };
Share this answer
paleGegg0 31-Dec-10 1:48am    
Thanks for the fast reply! I have done to my filelogging as you suggested.
Now im struggling with that query problem. Im truly new in linq world so I thought you could give tip for filtering. I have tried many ways without success. Following gives nothing but error:

XElement TraceMessages = XElement.Load(fullPath);

string filter = "Error"; //comes dynamically from ui in the future

//lets read only messages with category = "Error"
foreach (XElement x in TraceMessages.Elements("Messages").Elements("Message").Attributes(filter))
//add result to Ui component

I wouldnt bother you unless Im kinda hurry at the moment, I will meet client by end of the day :D
Vigneshb6 31-Dec-10 2:07am    
Nice One
paleGegg0 31-Dec-10 2:16am    
Hi again, thanks for the help! My app is working now. I really appreciate your effort and hope you good year! Its truly shame that I cannot give more that 5/5 for this answer :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900