PC Review


Reply
Thread Tools Rating: Thread Rating: 1 votes, 1.00 average.

C# and App.config

 
 
=?Utf-8?B?bXN1aw==?=
Guest
Posts: n/a
 
      4th Jan 2005
All,

I have a app.config which contains repeating tags as shown below:

<filelist>
<file>
<filetype>xxx</filetype>
<hex>yyyy</hex>
</file>

<file>
<filetype>aaa</filetype>
<hex>bbb</hex>
</file>
</filelist>

Can anyone suggest a way to read these values out and say load them into a
arraylist?

Thanks
Msuk
 
Reply With Quote
 
 
 
 
Peter Rilling
Guest
Posts: n/a
 
      4th Jan 2005
You say thay are in the app.config? How are they structured in the
app.config? Are they in the <appSettings> tag or some specialized tag.

Or is this a config file that has no relation to the app.config file used by
the Configuration namespace?

"msuk" <(E-Mail Removed)> wrote in message
news:5A5E0EC3-D2BD-4062-9043-(E-Mail Removed)...
> All,
>
> I have a app.config which contains repeating tags as shown below:
>
> <filelist>
> <file>
> <filetype>xxx</filetype>
> <hex>yyyy</hex>
> </file>
>
> <file>
> <filetype>aaa</filetype>
> <hex>bbb</hex>
> </file>
> </filelist>
>
> Can anyone suggest a way to read these values out and say load them into a
> arraylist?
>
> Thanks
> Msuk



 
Reply With Quote
 
Marc Scheuner [MVP ADSI]
Guest
Posts: n/a
 
      5th Jan 2005
>I have a app.config which contains repeating tags as shown below:
><filelist>
><file>
><filetype>xxx</filetype>
><hex>yyyy</hex>
></file>
>
><file>
><filetype>aaa</filetype>
><hex>bbb</hex>
></file>
></filelist>
>
>Can anyone suggest a way to read these values out and say load them into a
>arraylist?


You'd have to write your own custom config section handler - and it's
really not that frightening as it may sound!

Basically, you need a class which implements the
"IConfigurationSectionHandler " interface. This is a really simple
interface, which contains only a single method - "Create". That method
takes a XmlNode from your config files, and passes it to your class -
you then need to parse that and return some arbitrary object (e.g. an
ArrayList).

So your section handler would look something like:

public class MyFileListHandler : IConfigurationSectionHandler
{
public object Create(object parent, object ConfigContext, XmlNode
section)
{
ArrayList alsResult = new ArrayList();

// you get the node containing the <filelist>.....</filelist>
// so iterate over all <file> tags contained in it
XmlNodeList oListOfFiles = section.SelectNodes("//file");

// for each <file> node, grab its <filetype> and <hex>
// child nodes and store into your custom object
foreach(XmlNode oSingleFileNode in oListOfFiles)
{
// grab the child nodes
XmlNode oFileType =
oSingleFileNode.SelectSingleNode("//filetype");
string sFileType = oFileType.Value;

XmlNode oHex = oSingleFileNode.SelectSingleNode("//hex");
string sHex = oHex.Value;

alsResult.Add(new FileTypeHex(sFileType, sHex));
}

return alsResult;
}
}

And lastly, you need to define this custom section in your app.config
file, so that the .NET configuration system will know about it:

<configuration>
<configSections>
<section name="filelist"
type="YourAssembly.MyFileListHandler" />
</configSections>

<filelist>
<file>
<filetype>xxx</filetype>
<hex>yyyy</hex>
</file>
<file>
<filetype>aaa</filetype>
<hex>bbb</hex>
</file>
</filelist>
</configuration>

That should work and it's really quite easy, once you've done it and
you've gotten the hang of it !

Marc

================================================================
Marc Scheuner May The Source Be With You!
Berne, Switzerland m.scheuner -at- inova.ch
 
Reply With Quote
 
=?Utf-8?B?bXN1aw==?=
Guest
Posts: n/a
 
      5th Jan 2005
Hi,

thanks for your response, I am still a bit confused and was wondering if you
could help me.

public object Create(object parent, object ConfigContext, XmlNode
section)

I have created the function below and wanted to know what I need to pass to
the 'Create' i.e.

what is the 'parent', 'ConfigContext' and the 'XmlNode'.

Also how do I find the 'FileTypeHex' as my project does not build

alsResult.Add(new FileTypeHex(sFileType, sHex));

Thanks


"Marc Scheuner [MVP ADSI]" wrote:

> >I have a app.config which contains repeating tags as shown below:
> ><filelist>
> ><file>
> ><filetype>xxx</filetype>
> ><hex>yyyy</hex>
> ></file>
> >
> ><file>
> ><filetype>aaa</filetype>
> ><hex>bbb</hex>
> ></file>
> ></filelist>
> >
> >Can anyone suggest a way to read these values out and say load them into a
> >arraylist?

>
> You'd have to write your own custom config section handler - and it's
> really not that frightening as it may sound!
>
> Basically, you need a class which implements the
> "IConfigurationSectionHandler " interface. This is a really simple
> interface, which contains only a single method - "Create". That method
> takes a XmlNode from your config files, and passes it to your class -
> you then need to parse that and return some arbitrary object (e.g. an
> ArrayList).
>
> So your section handler would look something like:
>
> public class MyFileListHandler : IConfigurationSectionHandler
> {
> public object Create(object parent, object ConfigContext, XmlNode
> section)
> {
> ArrayList alsResult = new ArrayList();
>
> // you get the node containing the <filelist>.....</filelist>
> // so iterate over all <file> tags contained in it
> XmlNodeList oListOfFiles = section.SelectNodes("//file");
>
> // for each <file> node, grab its <filetype> and <hex>
> // child nodes and store into your custom object
> foreach(XmlNode oSingleFileNode in oListOfFiles)
> {
> // grab the child nodes
> XmlNode oFileType =
> oSingleFileNode.SelectSingleNode("//filetype");
> string sFileType = oFileType.Value;
>
> XmlNode oHex = oSingleFileNode.SelectSingleNode("//hex");
> string sHex = oHex.Value;
>
> alsResult.Add(new FileTypeHex(sFileType, sHex));
> }
>
> return alsResult;
> }
> }
>
> And lastly, you need to define this custom section in your app.config
> file, so that the .NET configuration system will know about it:
>
> <configuration>
> <configSections>
> <section name="filelist"
> type="YourAssembly.MyFileListHandler" />
> </configSections>
>
> <filelist>
> <file>
> <filetype>xxx</filetype>
> <hex>yyyy</hex>
> </file>
> <file>
> <filetype>aaa</filetype>
> <hex>bbb</hex>
> </file>
> </filelist>
> </configuration>
>
> That should work and it's really quite easy, once you've done it and
> you've gotten the hang of it !
>
> Marc
>
> ================================================================
> Marc Scheuner May The Source Be With You!
> Berne, Switzerland m.scheuner -at- inova.ch
>

 
Reply With Quote
 
Peter Rilling
Guest
Posts: n/a
 
      5th Jan 2005
You don't call Create at all, this is managed by the framework when it loads
the config file.

The article http://support.microsoft.com/default...b;en-us;309045,
might help you get started.


"msuk" <(E-Mail Removed)> wrote in message
news:411838D2-AB88-4B0F-AE38-(E-Mail Removed)...
> Hi,
>
> thanks for your response, I am still a bit confused and was wondering if

you
> could help me.
>
> public object Create(object parent, object ConfigContext, XmlNode
> section)
>
> I have created the function below and wanted to know what I need to pass

to
> the 'Create' i.e.
>
> what is the 'parent', 'ConfigContext' and the 'XmlNode'.
>
> Also how do I find the 'FileTypeHex' as my project does not build
>
> alsResult.Add(new FileTypeHex(sFileType, sHex));
>
> Thanks
>
>
> "Marc Scheuner [MVP ADSI]" wrote:
>
> > >I have a app.config which contains repeating tags as shown below:
> > ><filelist>
> > ><file>
> > ><filetype>xxx</filetype>
> > ><hex>yyyy</hex>
> > ></file>
> > >
> > ><file>
> > ><filetype>aaa</filetype>
> > ><hex>bbb</hex>
> > ></file>
> > ></filelist>
> > >
> > >Can anyone suggest a way to read these values out and say load them

into a
> > >arraylist?

> >
> > You'd have to write your own custom config section handler - and it's
> > really not that frightening as it may sound!
> >
> > Basically, you need a class which implements the
> > "IConfigurationSectionHandler " interface. This is a really simple
> > interface, which contains only a single method - "Create". That method
> > takes a XmlNode from your config files, and passes it to your class -
> > you then need to parse that and return some arbitrary object (e.g. an
> > ArrayList).
> >
> > So your section handler would look something like:
> >
> > public class MyFileListHandler : IConfigurationSectionHandler
> > {
> > public object Create(object parent, object ConfigContext, XmlNode
> > section)
> > {
> > ArrayList alsResult = new ArrayList();
> >
> > // you get the node containing the <filelist>.....</filelist>
> > // so iterate over all <file> tags contained in it
> > XmlNodeList oListOfFiles = section.SelectNodes("//file");
> >
> > // for each <file> node, grab its <filetype> and <hex>
> > // child nodes and store into your custom object
> > foreach(XmlNode oSingleFileNode in oListOfFiles)
> > {
> > // grab the child nodes
> > XmlNode oFileType =
> > oSingleFileNode.SelectSingleNode("//filetype");
> > string sFileType = oFileType.Value;
> >
> > XmlNode oHex = oSingleFileNode.SelectSingleNode("//hex");
> > string sHex = oHex.Value;
> >
> > alsResult.Add(new FileTypeHex(sFileType, sHex));
> > }
> >
> > return alsResult;
> > }
> > }
> >
> > And lastly, you need to define this custom section in your app.config
> > file, so that the .NET configuration system will know about it:
> >
> > <configuration>
> > <configSections>
> > <section name="filelist"
> > type="YourAssembly.MyFileListHandler" />
> > </configSections>
> >
> > <filelist>
> > <file>
> > <filetype>xxx</filetype>
> > <hex>yyyy</hex>
> > </file>
> > <file>
> > <filetype>aaa</filetype>
> > <hex>bbb</hex>
> > </file>
> > </filelist>
> > </configuration>
> >
> > That should work and it's really quite easy, once you've done it and
> > you've gotten the hang of it !
> >
> > Marc
> >
> > ================================================================
> > Marc Scheuner May The Source Be With You!
> > Berne, Switzerland m.scheuner -at- inova.ch
> >



 
Reply With Quote
 
Marc Scheuner [MVP ADSI]
Guest
Posts: n/a
 
      6th Jan 2005
>public object Create(object parent, object ConfigContext, XmlNode
>section)
>
>I have created the function below and wanted to know what I need to pass to
>the 'Create' i.e.
>
>what is the 'parent', 'ConfigContext' and the 'XmlNode'.


Those are just additional parameters that might get passed into the
Create function - not sure if they're really even being populated at
this time. But as another poster already mentioned - you never have to
call Create yourself! You just write the code, and make it available
to the .NET runtime - the runtime will then call your code when
needed.

>Also how do I find the 'FileTypeHex' as my project does not build
> alsResult.Add(new FileTypeHex(sFileType, sHex));


I didn't include it since it seemed like a totally simple thing to do
- it would be a small class you have to write yourself, to hold the
"filetyp" and the "hex" values. Something like:

public class FileTypeHex
{
private string m_sFileType = string.Empty;
private string m_sHex = string.Empty;

public FileTypeHex(string aFileType, string aHex)
{
m_sFileType = aFileType;
m_sHex = aHex;
}

public string FileType
{
get { return m_sFileType; }
}

public string Hex
{
get { return m_sHex; }
}
}


Marc
================================================================
Marc Scheuner May The Source Be With You!
Berne, Switzerland m.scheuner -at- inova.ch
 
Reply With Quote
 
Richard Grimes [MVP]
Guest
Posts: n/a
 
      6th Jan 2005
> public object Create(object parent, object ConfigContext, XmlNode
> section)
>
> I have created the function below and wanted to know what I need to pass
> to
> the 'Create' i.e.
> what is the 'parent', 'ConfigContext' and the 'XmlNode'.


You don't. You call ConfigurationSettings.GetConfig() passing the name of
the section according to the values in <configSections> for Marc's code this
will be "filelist" (Note that the type attribute of <section> should be a
fully qualified name, so it should include the full assembly name too, see
machine.config for examples.) The system then calls Create on your behalf.
The second parameter is only used by ASP.NET. It *may* call this method
twice, if machine.config has the section then Create will be called with
null for the first parameter, and the section in the last parameter. If the
app config has the section too, then Create is called again with the object
returned from the first call to Create in the first parameter and the
section from the app config in the last parameter. It is up to you what you
do with a non-null parent parameter, but usually the app data overwrites the
values from machine.config.

Richard
--
www.richardgrimes.com
my email (E-Mail Removed) is encrypted with ROT13 (www.rot13.org)


 
Reply With Quote
 
=?Utf-8?B?bXN1aw==?=
Guest
Posts: n/a
 
      7th Jan 2005
Hi,

Thanks for your response, after some experimenting I finally got it working.
The next problem I have is that if I create a class that has the GetConfig()
method and implements the IConfigurationSectionHandler and complie in to a
dll, then add this as a reference to say my aspx form, when I try to get any
values back I repeatly get 'Object reference not set to an instance of an
object'

The GetConfig() is a static method called as follows from my aspx form

string exe = ConfigurationSample2.SampleConfiguration.GetConfig.ExeName -
fails with 'Object reference not set to an instance of an object' what am I
doing wrong.

Thanks
Msuk

"Richard Grimes [MVP]" wrote:

> > public object Create(object parent, object ConfigContext, XmlNode
> > section)
> >
> > I have created the function below and wanted to know what I need to pass
> > to
> > the 'Create' i.e.
> > what is the 'parent', 'ConfigContext' and the 'XmlNode'.

>
> You don't. You call ConfigurationSettings.GetConfig() passing the name of
> the section according to the values in <configSections> for Marc's code this
> will be "filelist" (Note that the type attribute of <section> should be a
> fully qualified name, so it should include the full assembly name too, see
> machine.config for examples.) The system then calls Create on your behalf.
> The second parameter is only used by ASP.NET. It *may* call this method
> twice, if machine.config has the section then Create will be called with
> null for the first parameter, and the section in the last parameter. If the
> app config has the section too, then Create is called again with the object
> returned from the first call to Create in the first parameter and the
> section from the app config in the last parameter. It is up to you what you
> do with a non-null parent parameter, but usually the app data overwrites the
> values from machine.config.
>
> Richard
> --
> www.richardgrimes.com
> my email (E-Mail Removed) is encrypted with ROT13 (www.rot13.org)
>
>
>

 
Reply With Quote
 
Richard Grimes [MVP]
Guest
Posts: n/a
 
      7th Jan 2005
msuk wrote:
> The GetConfig() is a static method called as follows from my aspx form
>
> string exe =
> ConfigurationSample2.SampleConfiguration.GetConfig.ExeName - fails
> with 'Object reference not set to an instance of an object' what am I
> doing wrong.


Huh?

You should do this:

string exe =
System.Configuration.ConfigurationSettings.GetConfig("filelist");

Richard
--
www.richardgrimes.com
my email (E-Mail Removed) is encrypted with ROT13 (www.rot13.org)


 
Reply With Quote
 
=?Utf-8?B?bXN1aw==?=
Guest
Posts: n/a
 
      10th Jan 2005
All,

I am still getting the following error - 'Object reference not set to an
instance of an object' below is the code I am using.

I have the following App.Config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="MyCompany">
<section name="Filelist"
type="ConfigurationSample.SampleConfigurationHandler,MyAssembleyName" />
</sectionGroup>
</configSections>
<MyCompany>
<Filelist location="C:\Program Files\zzz\xxx.EXE">
<Files>
<add type=".jpg" hex="FFD8FFE0" />
<add type=".gif" hex="47494638" />
<add type=".bmp" hex="424D" />
</Files>
</Filelist>
</MyCompany>
</configuration>

I have one assembley (MyAssembleyName.dll) that contains the following code
to get the section I need:

using System;
using System.Configuration;
using System.Xml;

namespace ConfigurationSample {
public class SampleConfiguration
{
#region fields and properties

private string m_Exe;

public string ExeName
{
get { return m_Exe; }
set { m_Exe = value; }
}


#endregion


#region Constructors
public SampleConfiguration() {}
#endregion


public static SampleConfiguration GetConfig{
get {return
(SampleConfiguration)ConfigurationSettings.GetConfig("MyCompany/Filelist");}
}

internal void LoadValues(XmlNode node) {
XmlAttributeCollection attributeCollection = node.Attributes;
m_Exe = attributeCollection["location"].Value;

}

}

public class SampleConfigurationHandler : IConfigurationSectionHandler
{
public object Create(object parent, object context, XmlNode node)
{
SampleConfiguration config = new SampleConfiguration();
config.LoadValues(node);
return config;
}
}
}

In a separate solutuion I have a webform that has a reference to
MyAssembleyName.dll and I call the following method on the page load:

using ...
using ConfigurationSample;

try
{
string exeName = SampleConfiguration.GetConfig.ExeName
}
catch (Exception ex)
{
throw ex;
}

An exception is thrown saying - 'Object reference not set to an instance of
an object' what am I doing wrong?

Thanks
Msuk


"Richard Grimes [MVP]" wrote:

> msuk wrote:
> > The GetConfig() is a static method called as follows from my aspx form
> >
> > string exe =
> > ConfigurationSample2.SampleConfiguration.GetConfig.ExeName - fails
> > with 'Object reference not set to an instance of an object' what am I
> > doing wrong.

>
> Huh?
>
> You should do this:
>
> string exe =
> System.Configuration.ConfigurationSettings.GetConfig("filelist");
>
> Richard
> --
> www.richardgrimes.com
> my email (E-Mail Removed) is encrypted with ROT13 (www.rot13.org)
>
>
>

 
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
dll config and web.config and Label Expressions (binding label text to dll config settings) CSharpner Microsoft ASP .NET 0 9th Apr 2007 10:00 PM
DTDs for machine.config, web.config, security.config available ? =?Utf-8?B?UGF1bCBLZW5uZWR5?= Microsoft Dot NET Framework 1 5th Feb 2007 05:42 AM
sharedListeners config in app.config file doesn't recognize customlocation attribute RJ Microsoft Dot NET 0 1st Nov 2006 08:48 PM
using ReadXml to read exe.config file into Grid and update the exe.config using WriteXml hazz Microsoft C# .NET 0 7th Jul 2006 10:32 PM
Configuration Error - c:\winnt\microsoft.net\framework\v1.1.4322\Config\machine.config Ivan Microsoft ASP .NET 1 21st May 2004 05:18 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:49 PM.