Accesing and changing XML documents

R

Rodrigo Juarez

Hi

I have the next xml config file and I need to read and write key values, for
example, change the value of hibernate.connection.connection_string

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

<configSections>

<section name="nhibernate"
type="System.Configuration.NameValueSectionHandler, System,
Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>

</configSections>

<connectionStrings>

<add name="TiempoNet.Presupuesto.My.MySettings.PresupuestoConnectionString"

connectionString="Data Source=localhost\tiempo;Initial
Catalog=presupuesto;Persist Security Info=True;User ID=sa;Password=reloj"

providerName="System.Data.SqlClient" />

</connectionStrings>

<nhibernate>

<add key="hibernate.connection.provider"
value="NHibernate.Connection.DriverConnectionProvider"/>

<add key="hibernate.dialect" value="NHibernate.Dialect.MsSql2000Dialect"/>

<add key="hibernate.connection.driver_class"
value="NHibernate.Driver.SqlClientDriver"/>

<add key="hibernate.connection.connection_string"
value="Server=localhost\tiempo;initial catalog=presupuesto;User
Id=sa;password=reloj"/>

</nhibernate>

</configuration>



TIA



Rodrigo Juarez
 
S

Steven Cheng[MSFT]

Hello Rodrigo,

From your description, you're going to programmatically read and modify the
app.config file which has persisted some hibernate connectionstring pairs,
correct?

As the configuration fragment you've provided, you are using the
"System.Configuration.NameValueSectionHandler" class to handle the
"mhibernate" connection items. I would recommend you change the handler to
"System.Configuration.AppSettingsSection" class. Because
"NameValueSectionHandler" is a very raw handler class which doesn't provide
high level encapsualted interface for accessing each items in the
collection. The "AppSettingsSection" class is a well encaspulated
one(through the new .NET 2.0 configuration model) which can help us easily
read and modify items in those cofiguration sections handled by it. To use
it, you can simply change the section handler class to "AppSettingsSection"
as below:

======================
<configuration>
<configSections>
<section name="nhibernate"
type="System.Configuration.AppSettingsSection, System.Configuration,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
restartOnExternalChanges="false"/>

.........................
=======================


then, you can use the followin like code to enumerate values and add new
values in the configuration file:

===================================
private void btnConfig_Click(object sender, EventArgs e)
{
string output = string.Empty;



Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

AppSettingsSection section = config.GetSection("nhibernate")
as AppSettingsSection;


//enumerate all items in section
if (section != null)
{
foreach (string key in section.Settings.AllKeys)
{
output += "\r\n" + key + ": " +
section.Settings[key].Value;
}
}



//add new items
section.Settings.Add(new KeyValueConfigurationElement("newkey_"
+ DateTime.Now.Ticks, "newvalue"));

config.Save();

}
====================================


Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================



This posting is provided "AS IS" with no warranties, and confers no rights.
 
R

Rodrigo Juarez

Thanks a lot!!

Rodrigo

Steven Cheng said:
Hello Rodrigo,

From your description, you're going to programmatically read and modify
the
app.config file which has persisted some hibernate connectionstring pairs,
correct?

As the configuration fragment you've provided, you are using the
"System.Configuration.NameValueSectionHandler" class to handle the
"mhibernate" connection items. I would recommend you change the handler to
"System.Configuration.AppSettingsSection" class. Because
"NameValueSectionHandler" is a very raw handler class which doesn't
provide
high level encapsualted interface for accessing each items in the
collection. The "AppSettingsSection" class is a well encaspulated
one(through the new .NET 2.0 configuration model) which can help us easily
read and modify items in those cofiguration sections handled by it. To
use
it, you can simply change the section handler class to
"AppSettingsSection"
as below:

======================
<configuration>
<configSections>
<section name="nhibernate"
type="System.Configuration.AppSettingsSection, System.Configuration,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
restartOnExternalChanges="false"/>

.........................
=======================


then, you can use the followin like code to enumerate values and add new
values in the configuration file:

===================================
private void btnConfig_Click(object sender, EventArgs e)
{
string output = string.Empty;



Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

AppSettingsSection section = config.GetSection("nhibernate")
as AppSettingsSection;


//enumerate all items in section
if (section != null)
{
foreach (string key in section.Settings.AllKeys)
{
output += "\r\n" + key + ": " +
section.Settings[key].Value;
}
}



//add new items
section.Settings.Add(new KeyValueConfigurationElement("newkey_"
+ DateTime.Now.Ticks, "newvalue"));

config.Save();

}
====================================


Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================



This posting is provided "AS IS" with no warranties, and confers no
rights.
 
S

Steven Cheng[MSFT]

You're welcome :)

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top