How to convert XElement to lowercase?

This is my code. I essentially want it to return the descendants regardless of the string case.

XElement doc = XElement.Load(myReader.ReadSubtree());
IEnumerable<XElement> query1 = doc.Descendants(es + family).Descendants(es + parameterName.Trim());

If family is "Jones", but in the XML it's "jones", then nothing is found. The same thing happens with parameterName.

Is there any way to change XElement doc to lowercase? That way I can use String.ToLower on both family and parameterName.

I found this reply, but I don't know how to implement that solution here.

Since I rarely use linq, and I know that the previous code works, I tried going step-by-step and used the where extension, but it didn't return anything:

IEnumerable<XElement> query1 = doc.Descendants(es + family).Where(node => (string)node.Attribute(family) == family)
    .Descendants(es + parameterName.Trim()).Where(node => (string)node.Attribute(parameterName) == parameterName);

Since this change didn't return anything, I didn't go beyond that.

I also tried the following code, and I get a run error in the first where extension:

IEnumerable<XElement> query1 = 
    doc.Descendants(es + family).Where(node => node.Attribute(family).ToString().ToLower() == family.ToLower())
    .Descendants(es + parameterName.Trim()).Where(node2 => node2.Attribute(parameterName).ToString().ToLower() == parameterName.ToLower());

Answers


You could use the Equals method it allows ignoring case:

.Where(node => ((string)node.Attribute(family)).Equals(family,StringComparison.OrdinalIgnoreCase))

I used Ordinal comparison but there are others available in the StringComparison enum.


Need Your Help

java sort array list using bubblesort

java sorting

I have been working on this for the past 5 hours. for some reason I am not getting the result I am looking for. The method is supposed to sort an Arraylist of items by quantity using bubble sort. Not

Can the C preprocessor be used to tell if a file exists?

c++ c include preprocessor c-preprocessor

I have a very large codebase (read: thousands of modules) that has code shared across numerous projects that all run on different operating systems with different c++ compilers. Needless to say,

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.