Introduction
Finding values in an XML file can be done quickly with the use of XPath queries. The other day, we were writing our queries but found the need to find elements in the XML without being bothered with case sensitivity. Yeah, I know, you're right, but programming can be dirty sometimes.
Using the Code
The following extension methods enabled us to execute XPath-queries case-insensitive.
public static class XDocumentExtensions
{
public static XDocument TransformToLowerCaseElements(this XDocument value)
{
value.DescendantNodes().OfType<XElement>().ToList()
.ForEach(n => n.Name = n.Name.ToString().ToLower());
return value;
}
public static XElement XPathSelectElementIgnoreCase(this XDocument value, string xpath)
{
return value.TransformToLowerCaseElements().XPathSelectElement(xpath);
}
public static IEnumerable<XElement> XPathSelectElementsIgnoreCase
(this XDocument value, string xpath)
{
return value.TransformToLowerCaseElements().XPathSelectElements(xpath);
}
}
Sample Usage
Take the following XML as an example:
var xmlstring=@"=""=""
<Parents>
<Parent Name=""Chris"" Type=""father"">
<Child>
<Name>Jonathan</Name>
<AGE>13</AGE>
</Child>
</Parent>
</Parents>";
If we load this into an XDocument
and then transform it into lower case:
var xdoc = XDocument.Parse(xmlstring);
xdoc.TransformToLowerCaseElements();
The XML will look like:
="1.0"="utf-8"
<parents>
<parent Name="Chris" Type="father">
<child>
<name>Jonathan</name>
<age>13</age>
</child>
</parent>
</parents>
Now, knowing that all elements can be made lowercase in this XML, we are able to query it with our new extension method:
var xdoc = XDocument.Parse(xmlstring);
var age = xdoc.XPathSelectElementIgnoreCase("parents/parent/child/age").Value;
Points of Interest
At first, I also had the xpath
parameter in the XPathSelectElementIgnoreCase
transformed to lower case. I removed that because it made it impossible to use the attributes in the queries I was constructing.
These extension methods helped us a lot while writing our code and tests whilst the xsd wasn't yet definitive.
To find out more about case insensitivity in XPath queries, have a look at: