Can I store this in app.config?

B

Brett Romero

I'd like to store something such as the following the my app.config
file:

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

<appSettings>
<add key="DEBUG" value="true"/>
<add key="DEBUGCleanFile" value="true"/>
</appSettings>

<DataGridColumnCollection>
<AGrid value="true">
</AGrid>
<BGrid value="false">
<column1 value="true"/>
<column2 value="true"/>
<column3 value="true"/>
</Bgrid>
</DataGridColumnCollection>

<system.diagnostics>
</system.diagnostics>
</configuration>

I particularly want the DataGridColumnCollection section. I can read
the data fine out of the file using an XMLDocument object. However,
weird things happens. For example, on this line:

using(SqlConnection connection = new SqlConnection(connectionString))

I get this exception:

The type initializer for 'System.Data.SqlClient.SqlConnection' threw an
exception.

The app.config and above line are completely unrelated. When I remove
the DataGridColumnCollection, everything is fine but of course I loose
all of that data. For now, I'm using my own XML file but that is just
more files to haul around. Any suggestions on how I can stick the
DataGridColumnCollection section into app.config?

Thanks,
Brett
 
M

Michael Nemtsev

Hello Brett,

DataGridColumnCollection is a class, as I understand?
Then serialize it and keep it encoded just like a string

BR> I'd like to store something such as the following the my app.config
BR> file:
BR>
BR> <?xml version="1.0" encoding="utf-8" ?>
BR> <configuration>
BR> <appSettings>
BR> <add key="DEBUG" value="true"/>
BR> <add key="DEBUGCleanFile" value="true"/>
BR> </appSettings>
BR> <DataGridColumnCollection>
BR> <AGrid value="true">
BR> </AGrid>
BR> <BGrid value="false">
BR> <column1 value="true"/>
BR> <column2 value="true"/>
BR> <column3 value="true"/>
BR> </Bgrid>
BR> </DataGridColumnCollection>
BR> <system.diagnostics>
BR> </system.diagnostics>
BR> </configuration>
BR> I particularly want the DataGridColumnCollection section. I can
BR> read the data fine out of the file using an XMLDocument object.
BR> However, weird things happens. For example, on this line:
BR>
BR> using(SqlConnection connection = new
BR> SqlConnection(connectionString))
BR>
BR> I get this exception:
BR>
BR> The type initializer for 'System.Data.SqlClient.SqlConnection' threw
BR> an exception.
BR>
BR> The app.config and above line are completely unrelated. When I
BR> remove the DataGridColumnCollection, everything is fine but of
BR> course I loose all of that data. For now, I'm using my own XML file
BR> but that is just more files to haul around. Any suggestions on how
BR> I can stick the DataGridColumnCollection section into app.config?
BR>
BR> Thanks,
BR> Brett
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsch
 
B

Brett Romero

I'm not using that class. Let's call it MyDataGridColumnCollection. I
still get the sqlclient exception.

Thanks,
Brett
 
G

Guest

Brett,
If you want to store custom XML elements/ nodes in a configuration file,
they must either be recognized elements, or simple key=x value=y elements in
the appSettings section.
Otherwise, you need to declare a custom configuration section in your
config and have a custom sectionhandler to read it. Michael's comments about
serializing a class are correct.
Obviously, some study will be required but if this is the way you want to
store your custom data, it will be worth it.
Peter
 
S

Sygsky

Brett Romero пиÑал(а):
I'd like to store something such as the following the my app.config
file:

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

<appSettings>
<add key="DEBUG" value="true"/>
<add key="DEBUGCleanFile" value="true"/>
</appSettings>

<DataGridColumnCollection>
<AGrid value="true">
</AGrid>
<BGrid value="false">
<column1 value="true"/>
<column2 value="true"/>
<column3 value="true"/>
</Bgrid>
</DataGridColumnCollection>

<system.diagnostics>
</system.diagnostics>
</configuration>
...

You better look for the solution at the addrress

http://www.codeproject.com/csharp/readwritexmlini.asp

I use it for the near purposes and it works. May be you only would be
forced to change
from the appearance <column1 value="true"/> to a <add key="column1"
value="true"/> for all the entry in your custor section.
But in your program read results would be the same as you a need, I
suppose. In the same time this solution allows you to store the same
info in INI, XML and Registry in the same manner.
 
B

Brett Romero

Thanks Sygsky. The sections I create during one app session may not
be there in the next app session. For example, below AGrid has a
value of true. If it were false, it would have column element entries,
as does BGrid. The value and additional entries depend on if the user
has selected to show those particular columns.

<DataGridColumnCollection>
<AGrid value="true">
</AGrid>
<BGrid value="false">
<column1 value="true"/>
<column2 value="true"/>
<column3 value="true"/>
</Bgrid>
</DataGridColumnCollection>

The point is that the above will always be changing with respect to the
number of column elements under each xGrid parent and of course values
will change. There will always be an xGrid entry but the number
columns below it will vary. Right now, I'm using my own XML file and
everything works fine but I don't like dragging this additional file
around. If I use app.config, will it take a great amount of effort to
do the same thing I'm doing now (constantly removing/adding elements)?
I know using app.config just to change "value"s works is simple.

Thanks,
Brett
 
T

Tigger

In .Net 2.0 you can use the new settings architecture that supports
typed values.

I've used the type System.Xml.XmlDocument with no problems.

Tigger
 
B

Brett Romero

Tigger said:
In .Net 2.0 you can use the new settings architecture that supports
typed values.

I've used the type System.Xml.XmlDocument with no problems.

Tigger

Are you saying it can be used with app.config or with a custom XML
file? Can you give an example?

Thanks,
Brett
 
T

Tigger

Brett said:
Are you saying it can be used with app.config or with a custom XML
file? Can you give an example?

Thanks,
Brett

Its used with app.config but its a new schema for .Net 2.0 settings.

The easiest way is to use Studio 2005 and edit your settings via the
projects properties. This creates a wrapper class for your settings
which you can use to access the settings. e.g.

If you create a setting called MyXmlDocument of type System.XmlDocument
you can access it via...

Properties.Settings.Default.MyXmlDocument

Settings are stored in the app.config in configuration sections. If you
do the above your app.config file should be updated to include the
MyXmlDocument setting... something like
....
<setting name="MyXmlDocument" serializeAs="Xml">
<value>
<MyDocumentElement>Hello World</MyDocumentElement>
</value>
</setting>
....
This will be within a set of nodes which depend on your project and if
the variable is application or user level. The <configSections> at the
top defines this.

Tigger
 
B

Brett Romero

The thing I'm storing to file is a dictionary<string, dictionary<,>>
type. Parent nodes are in the first dictionary and child nodes in the
second. I'm only serializing values depending on the value in the
child dictionary. I can see this turning into something that will
takes days to get going with the app.config file.

Doesn't app.config want to serialize the entire class, say
MyXMLDocument? I don't want it all serialized. Just the particular
dictionary.

Thanks,
Brett
 
T

Tigger

How you take your objects and convert them to/from xml is up to you.

Note: I think you can use any xml serialisable object as a config
variable, but I believe generic collections are not xml serialisable.
So no matter what, you would have to create your own serialiser.

Tigger
 

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