Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SelectNodes with attribute containing apostrophe

Tags:

c#-4.0

xpath

I'm trying to use SelectNodes where an attribute is containing a text with apostrophes

The attribute is oor:path and the node looks like this:

<item oor:path="/org.openoffice.Office.Histories/Histories/org.openoffice.Office.Histories:HistoryInfo['PickList']/OrderList">

I have tried with this code (and failed)...

XmlNodeList xnList = xml.SelectNodes("/oor:items/item[contains(@oor:path, '[&apos;PickList&apos;]/OrderList')]", nsMgr);

Please Help!

// Anders

like image 893
Andis59 Avatar asked Nov 14 '22 15:11

Andis59


1 Answers

The &apos; entities are resolved into single quotes before the XPath parser can see them. Therefore, from its point of view, they cannot be distinguished from "real" single quotes.

You can delimit the argument to contains() with escaped double quotes and use single quotes in the expression:

XmlNodeList xnList = xml.SelectNodes(
    "/oor:items/item[contains(@oor:path, \"['PickList']/OrderList\")]", nsMgr);

Or, alternatively, using a verbatim string literal:

XmlNodeList xnList = xml.SelectNodes(
    @"/oor:items/item[contains(@oor:path, ""['PickList']/OrderList"")]", nsMgr);
like image 61
Frédéric Hamidi Avatar answered Dec 26 '22 01:12

Frédéric Hamidi