3

I would like to search node via Xpath case insensitve.

<Validation>
    <Presentation>
        <Slide Tag= "Test">
            <FontSize Value = "36"/>
        </Slide>
    </Presentation>
</Validation>

I've used this code

String xPath = string.Format("/Validation/Presentation/Slide[lower-case(@Tag)='{0}'][1]", "test");
XmlNode node = doc.DocumentElement.SelectSingleNode(xPath);

but it throws an XPath Exceptions: Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function. Where did I go wrong?

rageit
  • 3,513
  • 1
  • 26
  • 38
Matt3o
  • 377
  • 6
  • 15

3 Answers3

10

.NET doesn't support XPath 2.0.

So you can use this abomination:

    /Validation/Presentation/Slide[translate(@Tag,
   'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 
   'abcdefghijklmnopqrstuvwxyz')= 'test']

(I hard coded your value in for ease of testing with XPathBuilder.)

Steve Wellens
  • 20,506
  • 2
  • 28
  • 69
6

Have you tried this?
INFO: Use XPath to Perform a Case-Insensitive Search with MSXML: https://support.microsoft.com/en-us/kb/315719

use translate

doc.DocumentElement.selectSingleNode("/Validation/Presentation/Slide[translate(@Tag, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = 'test']")
Joel
  • 7,401
  • 4
  • 52
  • 58
Liu
  • 970
  • 7
  • 19
5

lower-case is XPath 2.0 function and .NET itself supports only Xpath 1.0 expressions, so you can't use it this way.

Alternatively you can use Linq2Xml:

var doc = XDocument.Load(@"your_file_Name");
var node = doc.XPathSelectElements("/Validation/Presentation/Slide")
               .FirstOrDefault(x => x.Attribute("Tag") != null 
                                    && String.Equals(x.Attribute("Tag").Value, "test",
                                                 StringComparison.CurrentCultureIgnoreCase));
Andrey Korneyev
  • 26,353
  • 15
  • 70
  • 71