Handling Special Characters in a FetchXML query


I was processing large quantities of FetchXML queries using Apache Kafka and ran into an issue where Kafka threw an Invalid XML error.

The way to solve this issue if you’re using .NET and C# is to encode the FetchXML:

                string name = HttpUtility.HtmlEncode(name);

                string fetch = $@"
                  <fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>
                  <entity name='customer'>
                    <attribute name='firstname' />
                    <attribute name='lastname' />
                    <order attribute='firstname' descending='false' />
                        <filter type='and'>
                          <condition attribute='firstname' operator='eq' value='{name}' />

This should encode the string correctly.

Leave a comment