473,320 Members | 2,161 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,320 software developers and data experts.

Parsing sections from individual Web.config files.

I have multiple websites running under Windows 2003 using IIS and ASP
..NET. I'd like to be able to take multiple Web.config files and parse
sections of them out for display in a datagrid or to place in some
other type of collection object.

Could someone please link me with samples that illustrate how to
extract sections in this manner?

I'd like to know both how to extract the info straight from the
Web.config file itself, as well as using .NET to extract it via the
website configuration (e.g. the ConfigurationManager object).

Any help would be greatly appreciated.

-- Alan

Nov 8 '06 #1
3 2091
It's not entirely clear what you need to do here. Bear in mind that a
web.config is an Xml document, and so you can load it into an XmlDocument
instance and use XPath queries to extract any element or node you want.
Further, there are various classes in the Configuration namespace that
enable you get configuration Sections. But, without knowing what your
web.config looks like and how it is set up, could not recommend anything
specific.
Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Da************@gmail.com" wrote:
I have multiple websites running under Windows 2003 using IIS and ASP
..NET. I'd like to be able to take multiple Web.config files and parse
sections of them out for display in a datagrid or to place in some
other type of collection object.

Could someone please link me with samples that illustrate how to
extract sections in this manner?

I'd like to know both how to extract the info straight from the
Web.config file itself, as well as using .NET to extract it via the
website configuration (e.g. the ConfigurationManager object).

Any help would be greatly appreciated.

-- Alan

Nov 8 '06 #2
Peter thanks nonetheless for your reply.

Here's part of my Web.config, much of the meat I've cut out for
simplicity's sake. I'm not familiar with using XPath queries, so maybe
if you can link me to an example I can figure it out. I know the
Web.config is a well-formed XML document, but I'm not yet familiar with
breaking out the fields and elements.

If you think a good book on XML would help feel free to recommend, I'm
all ears.

Thanks again.

---

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<!-- These list the sections inside this xml web.config -->
<configSections>
<section name="Newsletter" type="test" />
</configSections>

<system.web>
<!-- authentication method -->
<authentication mode="Forms">
<forms name="WebAuth" loginUrl="~/login.aspx" protection="All"
timeout="600" />
</authentication>

<trace enabled="false" pageOutput="false" localOnly="false" />

<!-- debug mode for aspx's. Should be true for dev and QA, false for
production -->
<compilation debug="true" defaultLanguage="c#" />
</system.web>
<WebSite>
<add key="IntegrationTest" value="IT" />
<Connections>
<add key="MySQLConnectionString"
value="server=mySQLServer;database=myDatabase;User name=myuser;Password=mypassword;"
/>

</Website>
</Connections>
</Configuration>

---

Nov 8 '06 #3
So far this is what I've done:

---

string filename = "C:\\web.config";
string xpathExpression = "//configuration/Connections";

XmlDocument document = new XmlDocument( );
document.Load(filename);

XmlTextWriter writer = new XmlTextWriter(Console.Out);
writer.Formatting = Formatting.Indented;

XmlNode node = document.SelectSingleNode(xpathExpression);
node.WriteTo(writer);

XmlNodeList elements =
document.SelectNodes("//configuration/Connections/add");

foreach (XmlElement element in elements)
{
string key = element.GetAttribute("key");
string val = element.GetAttribute("value");

Console.WriteLine("key: " + key + Environment.NewLine);
Console.WriteLine("value: " + val + Environment.NewLine);

}

---

I pieced together some examples I found in various articles to test
this out, I've managed to make it so that I can extract the key and
value elements of each "add" node contained in the "Connections" node.

Nov 8 '06 #4

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

Similar topics

0
by: Byrom, R (Rob) | last post by:
Hi, Does anyone know of a neat way to read-in config files? At the moment I'm using the ConfigParser module but I have to add sections to the config file to make it parseable ie
4
by: Fuzzyman | last post by:
There have been a couple of config file 'systems' announced recently, that focus on building more powerful and complex configuration files. ConfigObj is a module to enable you to much more *simply*...
5
by: Pat Ford | last post by:
Hi All; I'm working on a data acquisition system, the system runs on Linux, and the config files are Xml like. eg <CHANNELS> <META> Short Label,textbox,width=5,Alpha,required Long...
3
by: rh0dium | last post by:
Hi all, I have a file which I need to parse and I need to be able to break it down by sections. I know it's possible but I can't seem to figure this out. The sections are broken by <> with...
3
by: toton | last post by:
Hi, I have some ascii files, which are having some formatted text. I want to read some section only from the total file. For that what I am doing is indexing the sections (denoted by .START in...
9
by: Paulers | last post by:
Hello, I have a log file that contains many multi-line messages. What is the best approach to take for extracting data out of each message and populating object properties to be stored in an...
2
by: cmay | last post by:
I have an asp.net application at the root, and other applications suchs as /WebApp1, /WebApp2. By design, these web applications inherit the root application's web.config file. But, when you...
0
by: Andrea Anastasescu | last post by:
Hi everybody, Inside the web.config of my application I have some custom sections. I have my own handlers for these sections, and their content is in external files. My problem is that...
3
by: =?Utf-8?B?RGFuYQ==?= | last post by:
I am re-posting this message after registering my posting alias. When I specify an end tag for the clear element of namespaces in my web.config file, the parser error "Unrecognized element 'add'"...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.