Today while executing a plugin that has a method that search for a record of a custom entity based on its name, I started to see the following error:
Once I debugged the plugin, I found that the string I was using to search based on the name had an apostrophe(‘) and this was messing up with the Fetch XML apostrophes.
To correctly escape the special characters in the name I added the following regex line:
string FormatedText = Regex.Escape(RecordName);
This will add the correct c# scape to each of the special characters, for example, instead of having apostrophe (‘) it will be replaced with: \’
Hope it helps.