Many other questions here focus on the use of System.Configuration.ConfigurationManager... For my issue, I need to pull the connection string from a Spring object in order to determine the server connected to and remove any banners/warnings of using a test version of the application. The idea is that if the application connects to "sp1-...", the application is a production version.
How would one go about reading the object nested under the context of the App.config xml? Note that it exists outside the Hibernate object and thus (in my mind) must be stored within the Spring library variables.
Below is a section of my App.config for the application.
...
<spring>
<context>
<resource uri="config://spring/objects" />
</context>
<parsers>
<parser type="Spring.Data.Config.DatabaseNamespaceParser, Spring.Data" />
<parser type="Spring.Transaction.Config.TxNamespaceParser, Spring.Data" />
</parsers>
<objects xmlns="http://www.springframework.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.net/tx" xmlns:db="http://www.springframework.net/database">
<db:provider id="DbProvider" provider="SqlServer-2.0" connectionString="Data Source=st1-dskdb;Integrated Security=true;Database=AmericaMe_Test;" />
<!-- HIBERNATE OBJECT -->
<object id="MySessionFactory" type="Spring.Data.NHibernate.LocalSessionFactoryObject, Spring.Data.NHibernate12">
<property name="ExposeTransactionAwareSessionFactory" value="true" />
<property name="DbProvider" ref="DbProvider" />
...
Here is the way I would perform this operation if given a normal App.config setup:
// Remove the "WARNING Test Version" Label if not the production server (sp1...)
if (System.Configuration.ConfigurationManager.ConnectionStrings[0].ConnectionString.ToLower().Contains("sp1"))
{
WarningBox.Visible = false;
WarningLabel.Visible = false;
MainTabController.Dock = DockStyle.Fill;
}
EDIT Seems the XMLReader direction was the only way to go. A bit brutish in my opinion but here is the code I implemented which works:
// Remove the "WARNING Test Version" Label if not the production server (sp1...)
using (System.Xml.XmlReader appConfigReader = System.Xml.XmlReader.Create("AmericaMe.exe.config"))
{
while (appConfigReader.Read())
{
string appConfigLine = appConfigReader.Name;
if (appConfigLine.Contains("db:provider"))
{
string server = appConfigReader.GetAttribute(2);
if (server.Contains("Source=sp1-"))
{
WarningBox.Visible = false;
WarningLabel.Visible = false;
MainTabController.Dock = DockStyle.Fill;
}
break;
}
}
}