By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,905 Members | 1,585 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,905 IT Pros & Developers. It's quick & easy.

Parsing sections from individual Web.config files.

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.