问题描述:

I need to get <aw:Child1> elements using XPath from below xml by ignoring case.

<aw:Root xmlns:aw='http://www.test-works.com'>

<aw:Child uid='Test1'>child one data 1</aw:Child>

<aw:Child uid='Test2'>child one data 2</aw:Child>

<aw:Child uid='Test3'>child one data 3</aw:Child>

</aw:Root>

Here's my code, but it never returns any elements:

 string markup = @"

<aw:Root xmlns:aw='http://www.test-works.com'>

<aw:Child uid='Test1'>child one data 1</aw:Child>

<aw:Child uid='Test2'>child one data 2</aw:Child>

<aw:Child uid='Test3'>child one data 3</aw:Child>

</aw:Root>";

var reader = XmlReader.Create(new StringReader(markup));

var root = XElement.Load(reader);

var nameTable = reader.NameTable;

var namespaceManager = new XmlNamespaceManager(nameTable);

namespaceManager.AddNamespace("aw", "http://www.test-works.com");

var inputQuery = "./aw:Child[@uid='test1']"; //Auto generated

var elements = root.XPathSelectElements(inputQuery, namespaceManager);

foreach (var el in elements)

Console.WriteLine(el);

I get the results when I change the path to correct case

var inputQuery = "./aw:Child[@uid='Test1']";

But the elements to match are generated dynamically, and are always lowercase. Is there any way to ignore case while matching elements?

网友答案:

You can use the XPath 1.0 translate() function to replace all the uppercase characters to lowercase. In your case it will be:

var inputQuery = "./aw:Child[translate(@uid, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')='test1']";
相关阅读:
Top