Posted By: tgraupmann | Apr 9th @ 1:04 PM
page 1 of 1
Comments: 7 | Views: 1488
tgraupmann
tgraupmann
tgraupmann
I'm trying to use an XML entity in the App.config for a configuration value that appears in multiple appSettings.

I see the entity shows correctly in the VS 2008 xml editor, but when ConfigurationManager.AppSettings["SQLConnectionString"] returns, I see the literal text and not the resolved text "server=server\&SQLInstanceName;;database=Search;Integrated Security=SSPI;Max Pool Size=75; Min Pool Size=5".

Is this a bug in VS 2008 ConfigurationManager? Or is there a trick to make this work?


<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE configuration
[
  <!ENTITY SQLInstanceName "UnitTest">
]>
<configuration>
  <appSettings>

    <!-- Comment-->
    <add key="SQLConnectionString" value="server=server\&SQLInstanceName;;database=Search;Integrated Security=SSPI;Max Pool Size=75; Min Pool Size=5"/>
  </appSettings>
</configuration>

W3bbo
W3bbo
The Master of Baiters
It's not a bug, more like being feature-incomplete.

That said, why are you using entities (typically used for encoding purposes) rather than a simple format string?
Minh
Minh
WOOH! WOOH!
<?xml version="1.0" encoding="utf-8" ?>

<configuration>
  <appSettings>
<add key="SQLInstance" value="UnitTest" />
    <add key="SQLConnectionString" value="server=server\&SQLInstanceName;;database=Search;Integrated Security=SSPI;Max Pool Size=75; Min Pool Size=5"/>
  </appSettings>
</configuration>

string instance = ConfigurationManager.AppSettings["SQLInstance"];
string connStr = ConfigurationManager.AppSettings["SQLConnectionString"];

connStr = connStr.Replace("&SQLInstanceName", instance);

.... or ...

<configuration>
  <appSettings>
<add key="Environment" value="unit" />
    <add key="SQLConnectionString_unit" value="server=server\UnitTest;;database=Search;Integrated Security=SSPI;Max Pool Size=75; Min Pool Size=5"/>
    <add key="SQLConnectionString_prod" value="server=server\Production;;database=Search;Integrated Security=SSPI;Max Pool Size=75; Min Pool Size=5"/>
  </appSettings>
</configuration>

string env = ConfigurationManager.AppSettings["Environment"];
string connStr =
ConfigurationManager.AppSettings["SQLConnectionString_
" + env];

Sven Groot
Sven Groot
You can't have everything; after all, where would you put it?
If you're looking for a reason here, I think (but I'm just guessing) that DTD processing is probably disabled for config files, since DTDs can refer to external URLs so they can pose a security risk.
Sven Groot
Sven Groot
You can't have everything; after all, where would you put it?
tgraupmann wrote:
I bet if I loaded the config in an XmlDocument, it probably will resolve correctly.

Yes it will. With an XmlReader however you will need to enabled DTDs specifically.
page 1 of 1
Comments: 7 | Views: 1488