In the present day’s only a very brief article that may hopefully function a reminder to myself and a assist to others looking for methods to resolve the ‘Invalid XML’ error that’s generated by C# plugins and the likes utilising FetchXML for querying CRM information.
Fundamental Trigger
The principle trigger is just that you simply’ve incorrectly formatted your FetchXML question, a correctly formatted FetchXML will look one thing like one of many beneath examples –
C#
Uncooked FetchXML
For those who’re not assured in utilizing FetchXML then an excellent solution to get near what you need is to arrange an Superior Discover question in CRM, ensuring that it returns the data you’re anticipating then click on the ‘Obtain FetchXML’ button within the toolbar which will provide you with a duplicate of the FetchXML used to retrieve the data. You’ll have to reformat it barely for it to be usable in C# nevertheless it’s definitely simpler than writing it by hand.
Different Trigger (Particular Characters)
However what if you already know it’s all correctly formatted nevertheless it’s nonetheless throwing an error?
Properly, then you definately’re possible coming throughout a difficulty with particular characters, which is able to most frequently catch you out when filtering by fields equivalent to an Electronic mail Topic which may typically include ampersands and different nasty characters which is able to damage your FetchXML.
Fortunately there’s a quite simple resolution.
WebUtility
In your mission wrap the worth that’s inflicting the error with ‘WebUtility.HtmlEncode’ which is able to encode the values in order that it not causes points. Under is an instance the place I’ve modified the primary FetchXML screenshot above to make use of the HtmlEncode technique to keep away from these points.