1

In C# code, I am using xpath to query against the identity management database. I am trying to find the department that has a displayname "children's hospital".It is throwing error if the string has an apostrophe. - cannot filter as requested.
"/Department[DisplayName='children's hospital']" If I remove the apostrophe from children's in database as well as code, it is working fine. "/Department[DisplayName='childrens hospital']" Here is the C# code to retrieve from idms db.

 private string GetObjectID(string xpathQuery)
    {
        string objectGuid = string.Empty;
        string newxpath = string.Empty;
        int i = 0;           
        try
        {
            foreach (RmResource resource in Client.Enumerate(xpathQuery))
            {

                if (i == 0)
                {
                    return resource.ObjectID.ToString();
                }

            }
            return null;
        }
        catch
        {
            return null;
        }

    }
Grhm
  • 6,726
  • 4
  • 40
  • 64
kamsak
  • 11
  • 2
  • 1
    And what is the error/exception being thrown? – Peter Jun 09 '14 at 15:01
  • 1
    Show the actual code. – tnw Jun 09 '14 at 15:06
  • Are you trying to find entries like `HR'R` ? – Panagiotis Kanavos Jun 09 '14 at 15:07
  • 1
    As mentioned [here](http://stackoverflow.com/questions/3273143/php-xpath-dealing-with-apostrophe-single-quote-in-searched-text/3273435#3273435) you can't escape an apostrophe in xpath. That's not a C# issue, that's an XPath issue. – Panagiotis Kanavos Jun 09 '14 at 15:11
  • @kamsak Don't add code as a comment, it's completely unreadable. Edit your original post and add the code there. You also still need to add the complete error message you're receiving. Indicate which line in your code it's happening on. – tnw Jun 09 '14 at 15:20
  • You can't show us the error because `catch { return null; }` is discarding it. Remove that code from everything you ever write. – Dour High Arch Jun 09 '14 at 17:06

1 Answers1

0

As suggested in the link from @PanagiotisKanavos, the easisest way is using different quote character around corresponding part of the XPath. For example in your context :

//example using escaped double quotes
var xpath = "/Department[DisplayName=\"children's hospital\"]";
var result = GetObjectID(xpath);
har07
  • 88,338
  • 12
  • 84
  • 137