473,406 Members | 2,954 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Specialized.NameValueCollection

Hi all,
I've been scouring the internet for help with this problem and every
occurance i've seen reconstructs the problem but no one seems to have a
solution. Hoping that you guys can help me.

I have a vb.net windows forms project that is using a app.config file.
the contents of the app.config is staggered. see a snippet below:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<!-- tell .NET Framework to ignore CLR sections -->
<section name="frisbee"
type="System.Configuration.NameValueFileSectionHan dler, System,
Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<section name="frmBatching"
type="System.Configuration.NameValueFileSectionHan dler, System,
Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<section name="frmFiltering"
type="System.Configuration.NameValueFileSectionHan dler, System,
Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<section name="frmConfig"
type="System.Configuration.NameValueFileSectionHan dler, System,
Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<section name="frmSupporting"
type="System.Configuration.NameValueFileSectionHan dler, System,
Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</configSections>
<frisbee>
<add key="intErrorLogtype" value="1" />
<add key="EventLogKeyName" value="FRISBEE" />
<add key="SQLDBConnString" value="" />
<add key="sys_DBCheck" value="usp_CheckSQLConnection" />
<add key="Maximised" value="true" />
</frisbee>
<frmBatching>
<add key="height" value="" />
<add key="width" value="" />
<add key="top-left-position" value="" />
<add key="ImgDir" value="" />
<add key="BatchDir" value="" />
<add key="ImgInBatch" value="" />
<add key="BatchNo" value="" />
<add key="Maximised" value="true" />
</frmBatching>
Now to read values out of this staggered app.config file i use the
specialised.namevaluecollection in the following manner

Dim objConfigNV As New Specialized.NameValueCollection
Dim strConfigValue As String = Convert.ToString(vbNullString)

objConfigNV = CType(ConfigurationSettings.GetConfig(strIDicName) ,
Specialized.NameValueCollection)

strConfigValue = Convert.ToString(objConfigNV(strKeyName))

And this works fine.

Now.. the problem..

to write a value back to the app.config file using the same
Specialized.NamevalueCollection in the following manner:

'initialise collection object of app.config
Dim objNameValue As New Specialized.NameValueCollection

'app.ocnfig is staggered. this gets the relevant sectoin on
file
objNameValue =
CType(ConfigurationSettings.GetConfig("frmSupporti ng"),
Specialized.NameValueCollection)

'this sets the the new value of a key
objNameValue(strKey) = strValue
Generates an exception: Collection is read-only
Does ANYONE have any ideas how to create an instance of a
NameValueCollection that is not read-only so that I can write back to my
app.config file.
Any help on this would be greatly appreciated.

Kerr

*** Sent via Developersdex http://www.developersdex.com ***
Nov 21 '05 #1
4 7794
App.config file was never meant to be written to using the
NameValueCollection. It was not meant to store user settings. The
only way to write back to it would be to use the methods in the
System.Xml namespace.

What I typically do is design a class that models all the settings I
need and then I make that class serializable. Then I just need to
serialize and deserialize to/from the xml file and I have an object
with all my settings.

Nov 21 '05 #2
Chris,
Thanks for your reply. Unfortunately you've over estimated my .net
knowledge.

What you've described makes sense but I need to see a practical example
of what you describe for me to be able to implement a solution that
works.

I hear what your saying about not using the namevaluecollection to write
to the app.config file but because I am extending the use of the
app.config file by adding in the additional sections I was pointed at
using this method by other forums.

Because I am not sure how to implement your suggestion my question still
stands, which is, do you know of a way to create a NON-READ-ONLY version
of the NameValueCollection.

Cheers

Kerr

*** Sent via Developersdex http://www.developersdex.com ***
Nov 21 '05 #3
There is no way to create a NON-READ-ONLY NameValueCollection that I
know of. But the method below is just a simple.

What I mean is that I create a class with properties that hold the
values that I want to store, then use the XmlSerializer to save that
class with it's values to a file.

Here is a simple example.

Create a new class file and add the code at the end of this post. Then
add a Sub Main or sub with the following code:

Public Sub Main()

'Create an instance of the MySettings class
Dim settings As New MySettings

'Set some of the properties
settings.Frisbee.Maximised = False
settings.frmBatching.Width = 44

'Save the configuration to an .xml file
MySettings.Save("c:\MySettings.xml", settings)
'To load the data from the file use this code:
Dim settings2 As MySettings =
MySettings.Load("c:\MySettings.xml")

'Print the value of the Width property:
Console.WriteLine(settings2.frmBatching.Width.ToSt ring)

'Change the value of the width property
settings2.frmBatching.Width = 100

'Resave the file
MySettings.Save("c:\MySettings.xml", settings2)

End Sub
'Note that the classes are marked as Serializable() and that the
serializer
'will only save the properties and fields that are public. I used
public fields
'in this case, but you could use full properties.

'The Load and Save method are Shared methods for convenience.
'******** MySettings.Vb *******************

Imports System.Xml.Serialization
Imports System.IO

<Serializable()> _
Public Class MySettings

Public Frisbee As FrisbeeClass
Public frmBatching As frmBatchingClass

Public Sub New()
Frisbee = New FrisbeeClass
frmBatching = New frmBatchingClass
End Sub

Shared Function Load(ByVal fname As String) As MySettings
Dim sr As StreamReader

Try
sr = New StreamReader(fname)
Dim xs As New XmlSerializer(GetType(MySettings))
Return DirectCast(xs.Deserialize(sr), MySettings)
Finally
sr.Close()
End Try

End Function

Shared Sub Save(ByVal fname As String, ByVal obj As MySettings)
Dim sw As StreamWriter

Try
sw = New StreamWriter(fname)
Dim xs As New XmlSerializer(GetType(MySettings))

xs.Serialize(sw, obj)
Finally
sw.Close()
End Try

End Sub
End Class

<Serializable()> _
Public Class FrisbeeClass
Public intErrorLogType As Integer
Public EventLogKeyName As String = String.Empty
Public SQLDBConnString As String = String.Empty
Public sys_DBCheck As String = String.Empty
Public Maximised As Boolean
End Class

<Serializable()> _
Public Class frmBatchingClass
Public Height As Integer
Public Width As Integer
Public TopLeft As Integer
Public ImgDir As String = String.Empty
Public BatchDir As String = String.Empty
Public ImgInBatch As String = String.Empty
Public BatchNo As Integer
Public Maximised As Boolean
End Class

'************ END MySettings.vb *******************

Nov 21 '05 #4
Chris,
thanks for the post and the code. Your implementation works and I
understand how it works which is even better. However, I am not sure if
its something i've done wrong but whenever writing back (save) to the
xml document the structure of the document changes and then my entire
app stops working.

I am getting to the stage where i am thinking it is easier to do this
sort of dynamic configuration stuff through a database rather than the
app.config file. I can't spend too much longer trying to get this to
work.

Chris, thanks for your help on this anyway.

Kerr

*** Sent via Developersdex http://www.developersdex.com ***
Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Walter Zydhek | last post by:
I am having a problem using the NameValueCollection type. If I remove one of the items while iterating through an collection of this type, I end up with an exception. This exception is:...
5
by: anon | last post by:
I need a little clarity in the NameValueCollection. Within the MSDN .NET 1.1 Framework help is says: "This collection is based on the NameObjectCollectionBase class. However, unlike the...
0
by: Nicole_new C# Programmer | last post by:
I am creating an application where I have 3 listboxes. The 1st holds the attributes of our products like division, group, etc. The 2nd listbox holds the values attach to each attribute like...
7
by: davidw | last post by:
I always use NameValueCollection. But I read an article says the only differece between Hashtable and NameValueCollection is that NameValueCollection could accept more than one value with same key?...
1
by: Mark Miller | last post by:
I just recently started getting the above error on a page I am posting MULTIPART/FORM-DATA. We have SoftArtisans FileUp component and Filter installed on the server in question and up until a day...
0
by: Yuri Vanzine | last post by:
Saw this requested awhile ago on Matthew Reynolds's blog here: http://www.dotnet247.com/247reference/msgs/11/57944.aspx Problem: often times I deal with querystring-formatted strings w/o the...
3
by: Mike Logan | last post by:
We are trying to serialize the Request.ServerVariables collection (NameValueCollection) to an XML formatted string, to insert into a database. The problem we are running into is that most of the...
3
by: Phil C. | last post by:
Hi. I need to translate the C# statement: if( this.validatorAssignments != null && this.validatorAssignments.HasKeys() ) where validatorAssignments is an instance of...
0
by: John Grandy | last post by:
My business-layer class has a method that returns a System.Collections.Specialized.NameValueCollection I wrapped this business-layer class in a Web Service. In an ASP.NET project, when I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.