Parsing sections from individual Web.config files.

D

DarthContinent

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
 
G

Guest

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
 
D

DarthContinent

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;Username=myuser;Password=mypassword;"
/>

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

---
 
D

DarthContinent

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);

}
 

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

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top