PC Review


Reply
Thread Tools Rate Thread

Accesing and changing XML documents

 
 
Rodrigo Juarez
Guest
Posts: n/a
 
      16th Feb 2007
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


 
Reply With Quote
 
 
 
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      19th Feb 2007
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/subscripti...ult.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/subscripti...t/default.aspx.

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



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

 
Reply With Quote
 
Rodrigo Juarez
Guest
Posts: n/a
 
      19th Feb 2007
Thanks a lot!!

Rodrigo

"Steven Cheng[MSFT]" <(E-Mail Removed)> escribió en el mensaje
news:(E-Mail Removed)...
> 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/subscripti...ult.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/subscripti...t/default.aspx.
>
> ==================================================
>
>
>
> This posting is provided "AS IS" with no warranties, and confers no
> rights.
>



 
Reply With Quote
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      19th Feb 2007
You're welcome

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


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

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Changing # of Recent Documents Kathy Microsoft Word Document Management 7 14th Aug 2008 10:04 AM
CHANGING DOCUMENTS =?Utf-8?B?SmFuaWNl?= Microsoft Word Document Management 6 5th Sep 2006 10:55 PM
changing of my documents for different uses of the computer Stefano Spyware Discussion 2 25th Aug 2005 05:20 AM
RE: Accesing Excel documents with .Net =?Utf-8?B?UmFtb24=?= Microsoft Dot NET 0 6th Aug 2004 01:27 PM
Changing Name of c:documents and settings Mary Lokken Windows XP Setup 0 7th Aug 2003 04:12 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:09 PM.