DukeOfMarmalade
DukeOfMarmalade

Reputation: 2788

'Where' clause in LINQ To XML not selecting anything

I am just trying to read some details from an XML file, part of which looks like this:

<appender name="FILE" class="applications.core.logging.CustomFileAppender">
    <param name="File" value="C:\\Logs\\File.log"/>
    <param name="MaxBackupIndex" value="5"/>
</appender>
<appender name="FILE" class="applications.core.logging.CustomFileAppender">
    <param name="File" value="C:\\Logs\\File2.log"/>
    <param name="MaxBackupIndex" value="17"/>
</appender>
<appender name="FILE" class="applications.core.logging.CustomFileAppender">
    <param name="File" value="C:\\Logs\\File3.log"/>
    <param name="MaxBackupIndex" value="98"/>
</appender>

I have several of these 'appender' nodes in my XML file. The following code loops through each 'appender' node. Within each 'appender' I want to pick out the param node with name "File" and check if the value is equal to what I am looking for.

foreach (XElement node in XmlFile.Descendants("appender"))
        {
            IEnumerable<XElement> elements = from el in node.Elements("param") 
                                             where el.Attribute("value").ToString().Equals("C:\\Logs\\File.log")) 
                                             select el;

            foreach (XElement el in elements)
            {
                Console.WriteLine("Found it " + el.Name);
                // Now read value for MaxBackupIndex
            }
        }

However my code is not printing anything out, so I think possibly the 'where' part of my LINQ query is incorrect, can anybody spot where I am going wrong?

Upvotes: 3

Views: 1708

Answers (3)

Charleh
Charleh

Reputation: 14002

You are escaping your slashes in your where clause, but the XML already has double slashes - try adding an @ at the front of the strings in your where clause to escape the escaping (and take the string literally) and see if that helps:

e.g.

el.Attribute("value").ToString().Equals(@"C:\\Logs\\File.log")) 

Upvotes: 4

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726599

Attributes inside your XML file contains doubled slashes, but you are matching single ones. To match a double slash in an XML/TXT file, you need four slashes in your C# file.

Upvotes: 8

Jon Skeet
Jon Skeet

Reputation: 1500665

You're calling XAttribute.ToString(). Try using XAttribute.Value instead, or just cast to a string. ToString() will return value="C:\\Logs\\File.log" - the name and the value - whereas you just want the value:

var elements = from el in node.Elements("param")
               where (string) el.Attribute("value") == "C:\\Logs\\File.log"
               select el;

Personally I wouldn't bother with a query expression here, btw:

var elements = node.Elements("param")
         .Where(el => (string) el.Attribute("value") == "C:\\Logs\\File.log")

EDIT: Additionally, as others have said, you want to have single backslashes in the XML file.

Upvotes: 8

Related Questions