none
Searching XML with XPath RRS feed

  • Question

  • I'm having trouble searching an xml-file with

    I have a XML file with the following content:

    <D20Character>
       <textstring name="USAGE_Ice Dragon&apos;s Teeth">
          false
       </textstring>
       <textstring name="USAGE_Explosive Pyre">
          false
       </textstring>
    </D20Character>

    When I search for the first element with the following code, I get no results (node == null)

      xPath = @"/D20Character/textstring[@name='USAGE_Ice Dragon&apos;s Teeth']";
      node = nav.SelectSingleNode(xPath);<br />
    


    Finding the second node is no problem, the problem seems to be the &apos; construct. I suspect that it must be prefixed somehow, but I have not found a solution that actually works yet (I've tried with &&apos; and &amp;apos;)

    How do I search for this xml-element?
    • Edited by Nilcat Monday, October 5, 2009 2:34 PM Added whitespace
    Monday, October 5, 2009 2:33 PM

Answers

  • Understood.  I thought your XML attribute contained the string "USAGE_Ice Dragon's Teeth" when it actually contains the string "USAGE_Ice Dragon&apos;s Teeth".

    In my new test, I successfully retrieved a value using the XPath of
    /D20Character/textstring[@name="USAGE_Ice Dragon&apos;s Teeth"]

    What type of variable is "nav"?  I used SelectSingleNode from the XML Document variable.

    Monday, October 5, 2009 3:57 PM
  • Eureka!

    I isolated the offending code in a small test program and stumbled on the answer:

    I've found out how to do it. If the xPath contains the single quote character ('), then SelectSingleNode (and SelectNodes) can find the node with the aphostrope escape sequence (&apos;)

    So the following xPath:

    xPath = "/D20Character/textstring[@name=\"USAGE_Ice Dragon's Teeth\"]"; // Contains the single quote character and escaped double quotes qround the attribute name

    Finds this element:
    <D20Character>
       <textstring name="USAGE_Ice Dragon&apos;s Teeth"> <!-- Contains no single quotes - only the double quotes around the name --->
          false
       </textstring>
    </D20Character>


    Thanks for your time!

    BTW: "nav" is a XPathNavigator

    Cato
    • Marked as answer by Nilcat Monday, October 5, 2009 5:36 PM
    Monday, October 5, 2009 5:36 PM

All replies

  • See
    http://kushalm.com/the-perils-of-xpath-expressions-specifically-escaping-quotes

    Basically you need to enclose single quotes with double quotes and double quotes with single quotes.  Quite an annoying process but not so bad if you have a function for it.

    • Proposed as answer by Grax Monday, October 5, 2009 3:07 PM
    • Unproposed as answer by Nilcat Monday, October 5, 2009 3:31 PM
    Monday, October 5, 2009 3:05 PM
  • Thanks for the quick reply Grax, but the problem here is not the quotes, but the presence of the "&apos;" string in the attribute I'm searching for. If you look at the xml snippet above, the attribute value contains the string "&apos;" and not any actual quotes. My xpath consequently contains only single quotes around the attribute value.

    This is further evidenced by the fact that I can easily find the sedcond attribute with this code

      xPath = @"/D20Character/textstring[@name='USAGE_Explosive Pyre']";
      node = nav.SelectSingleNode(xPath);<br />
    


    Cato
    Monday, October 5, 2009 3:31 PM
  • Understood.  I thought your XML attribute contained the string "USAGE_Ice Dragon's Teeth" when it actually contains the string "USAGE_Ice Dragon&apos;s Teeth".

    In my new test, I successfully retrieved a value using the XPath of
    /D20Character/textstring[@name="USAGE_Ice Dragon&apos;s Teeth"]

    What type of variable is "nav"?  I used SelectSingleNode from the XML Document variable.

    Monday, October 5, 2009 3:57 PM
  • Eureka!

    I isolated the offending code in a small test program and stumbled on the answer:

    I've found out how to do it. If the xPath contains the single quote character ('), then SelectSingleNode (and SelectNodes) can find the node with the aphostrope escape sequence (&apos;)

    So the following xPath:

    xPath = "/D20Character/textstring[@name=\"USAGE_Ice Dragon's Teeth\"]"; // Contains the single quote character and escaped double quotes qround the attribute name

    Finds this element:
    <D20Character>
       <textstring name="USAGE_Ice Dragon&apos;s Teeth"> <!-- Contains no single quotes - only the double quotes around the name --->
          false
       </textstring>
    </D20Character>


    Thanks for your time!

    BTW: "nav" is a XPathNavigator

    Cato
    • Marked as answer by Nilcat Monday, October 5, 2009 5:36 PM
    Monday, October 5, 2009 5:36 PM
  • So the issue turns out to be related to quotes. I think Grax's inputs deserve being marked as answer. :)

    Regards,
    Jialiang Ge
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Tuesday, October 6, 2009 5:32 AM
    Moderator