An error occurred while parsing EntityName

A bare ampersand (&) in text or in an attribute.

The message, as .NET reports it
An error occurred while parsing EntityName. Line 1, position 17.

Why it happens

  • An unescaped &: a company name, a URL query string.
  • XML built with string concatenation or interpolation instead of an XML API.

How to fix it

Write & for every literal ampersand. XmlWriter, XElement and XmlSerializer escape it for you; SecurityElement.Escape does it for a string.

.NET throws an XmlSchemaValidationException at the first schema error unless XmlReaderSettings.ValidationEventHandler is set, which then receives every one with its line and position. A well-formedness error is an XmlException and ends reading whatever the settings.

The same problem in xmlvalid

xmlvalid validates with libxml2, which reports it as xmlParseEntityRef: no name. It checks the whole document in your browser and lists every schema error with its line and column, not only the first.

xmlParseEntityRef: no name

Example

Reports the error
<company>Smith & Sons</company>
Fixed
<company>Smith &amp; Sons</company>