tags:

views:

87

answers:

4

Hello

I have web.config file from some application. It is located in some random location. I have to parse this web.config file (get all keys names and values). I tried to use ConfigurationManager class in order to get those data however, it throws exception when I try to get some Sections (Configuration->GetSection('section name')). It throws exception because I do not have dll that this section points to (because I have only web.config not whole application). It seems that GetSection method check underlying dll in order to get more info, but I just need value (name of dll).

What can I do, to turn off this mechanism, do you know other simple solutions to get it done ?

+4  A: 

You are just going to have to use XmlDocument or XDocument (3.5) to parse the file.

Strelok
+2  A: 

If you just want to read the text, and not do any web.config-specific processing, use the fact that a .config file is XML, and use your favourite usual way of reading and parsing XML.

AakashM
+2  A: 

Web.Config files are just XML and an be read using a number of .Net XML objects. Below are a couple of methods.

Tutorial on reading an XML file using XmlTextReader http://support.microsoft.com/kb/307548

Tutorial on reading an XML file using LinqToSQL http://www.mssqltips.com/tip.asp?tip=1524

Gavin Draper
+3  A: 

have a look at this post. seems this is what your are looking for

http://stackoverflow.com/questions/4738/using-configurationmanager-to-load-config-from-an-arbitrary-location

ajay_whiz
+1. The less code you have to write, and closer you can get to an existing solution, the better.
Merlyn Morgan-Graham
ExeConfigurationFileMap configurationFile = new ExeConfigurationFileMap(); configurationFile.ExeConfigFilename = 'path to web.config file'; Configuration config = ConfigurationManager.OpenMappedExeConfiguration( configurationFile ,ConfigurationUserLevel.None); ConfigurationSection cs= config.GetSection('sectionName');In this solution I still get exception when I use GetSection method
Darqer
check to see if it loaded your file. You should check the HasFile property where true means it came from a file.
ajay_whiz
Yes, web.config file is loaded. I can take some entries from this web.config, but there are some entries that throw excpetions. Exception message: An error occurred creating the configuration section handler for plugIns: Could not load file or assembly ... The system cannot find file specified (web.config line 18)
Darqer
it seems that web.config file as some sections which require a section handler (a separate dll) to parse. Try referencing those assemblies as well.
ajay_whiz
I cannot because I have only web.config file
Darqer
then in that case it won't be possible to use the GetSection method 'coz the section handlers are not present. For this you will have to use any Xml reader as suggested by @Strelok
ajay_whiz