17

In my AppSettings in web.config, I have something like this:

<appSettings>
    <add key="ExternalSystemUrl" value="http://example.com/page.aspx?id={0}&action=eat&object=bacon" />
</appSettings>

However, it seems that when an ampersand (&) is included in an AppSettings value, ASP.NET throws the following error:

An error occurred while parsing EntityName

Why does this happen, and how can I include URLs like this in App.config?

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Brandon
  • 13,956
  • 16
  • 72
  • 114

3 Answers3

50

Replace & with &amp; (escape it):

<add
    key="ExternalSystemUrl"
    value="http://example.com/page.aspx?id={0}&amp;action=eat&amp;object=bacon" />

That's the common requirement for any valid XML file.

See Where can I get a list of the XML document escape characters?

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
abatishchev
  • 98,240
  • 88
  • 296
  • 433
6

You can Try using &amp; instead.

Wicked Coder
  • 1,128
  • 6
  • 8
4

In XML an ampersand tells the parser "the data immediately following this ampersand is an entity which needs to be translated." If the data immediately following is not a valid XML entity, then you get this error. If possible, use &amp; for your ampersand within the XML.

Chris White
  • 1,068
  • 6
  • 11