KnowDotNet NetRefactor

Additional information: '<', hexadecimal value 0x3C, is an invalid attribute character Errors in Parsing AppConfig Files with Embedded HTML

by Les Smith
Print this Article Discuss in Forums

How do I get around an error being thrown by ConfigurationSettings.appSettings when I have a "<...>" embedded in the value of an appConfig key?  Although the "<...>" is enclosed in double quotes, the .NET Framework raises an exception saying that it cannot be parsed.

I need to put a PGP Encryption UserID in my appConfig file, such as shown below.

  <add key="PGPUSERID" value="HHISW <lsmith@hhisoftware.com>" />

When I used the key shown above, an exception was thrown complaining about the embedded "<", even when I was accessing a different key.  So I changed the key string to look like the following, thinking that I could fool the parser. (space in the HTML to make it display correctly in the article)

  <add key="PGPUSERID" value="HHISW &l t;lsmith@hhisoftware.com&g t;" />

Now, I get the value, but instead of the "&  lt;" and "& gt;", I need them translated to "<" and ">" respectively.  Thanks to a co-worker,
Steven Specht, I was advised to change the retrieval code that I was using to retrieve keys from the following:

   ConfigurationSettings.AppSettings(key)

to the following:

   System.Web.HttpUtility.HtmlDecode(ConfigurationSettings.AppSettings(key))

Now, the appConfig file will parse fine.  Note, that you must use the HtmlDecode to retrieve any key, not just the one that has the HTML in it, otherwise you will get a parsing exception.

Writing Add-Ins for Visual Studio .NET
Writing Add-ins for Visual Studio .NET
by Les Smith
Apress Publishing