473,832 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is this the best way to read the web.config file? XPath didn't work ;-(

Hello,

I have a need to read the web.config file from another web site. I am
trying get the value of the "theme" attribute of the "pages" node.

I initially tried to do this by loading the file into an XmlDocument and
using SelectSingleNod e with a path like
"/configuration/system.web/pages", but no matter what variation of the
path I tried, this gave a null object.

I ended up with...

XmlAttributeCol lection pagesAttributes = webConfig.First Child.NextSibli ng.FirstChild["pages"].Attributes;

which works, but looks a little fragile to me. I would really prefer to
use a path. Anyone any advice? Thanks in advance.

--
Alan Silver
(anything added below this line is nothing to do with me)
Dec 7 '05 #1
8 3902
This works fine for me:
XmlDocument document = new XmlDocument();
document.Load(S erver.MapPath(" ~/web.config"));
XmlNode node =
document.Select SingleNode("/configuration/system.web/pages");
string theme = node.Attributes["theme"].Value;

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Alan Silver" <al*********@no spam.thanx> wrote in message
news:hE******** ******@nospamth ankyou.spam...
Hello,

I have a need to read the web.config file from another web site. I am
trying get the value of the "theme" attribute of the "pages" node.

I initially tried to do this by loading the file into an XmlDocument and
using SelectSingleNod e with a path like "/configuration/system.web/pages",
but no matter what variation of the path I tried, this gave a null object.

I ended up with...

XmlAttributeCol lection pagesAttributes =
webConfig.First Child.NextSibli ng.FirstChild["pages"].Attributes;

which works, but looks a little fragile to me. I would really prefer to
use a path. Anyone any advice? Thanks in advance.

--
Alan Silver
(anything added below this line is nothing to do with me)

Dec 7 '05 #2
<karl@REMOVE.?. invalid> writes
This works fine for me:
XmlDocument document = new XmlDocument();
document.Load(S erver.MapPath(" ~/web.config"));
XmlNode node =
document.Selec tSingleNode("/configuration/system.web/pages");
string theme = node.Attributes["theme"].Value;


I tried that one already, and got an "Object reference not set to an
instance of an object" exception on the last line. I have no idea why as
the web.config file is being loaded (the other way of doing it got the
attribute value fine), so I don't know why this doesn't work.

Any ideas? Thanks.

--
Alan Silver
(anything added below this line is nothing to do with me)
Dec 7 '05 #3
I think it's something wrong in your code. Debug it is the only idea I can
offer. the attribute name is case sensitive. I can't say if node is null
or Attributes["theme"] is null, but if you step through it ought to be
obvious enough.

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Alan Silver" <al*********@no spam.thanx> wrote in message
news:Nk******** ******@nospamth ankyou.spam...
<karl@REMOVE.?. invalid> writes
This works fine for me:
XmlDocument document = new XmlDocument();
document.Load(S erver.MapPath(" ~/web.config"));
XmlNode node =
document.Sele ctSingleNode("/configuration/system.web/pages");
string theme = node.Attributes["theme"].Value;


I tried that one already, and got an "Object reference not set to an
instance of an object" exception on the last line. I have no idea why as
the web.config file is being loaded (the other way of doing it got the
attribute value fine), so I don't know why this doesn't work.

Any ideas? Thanks.

--
Alan Silver
(anything added below this line is nothing to do with me)

Dec 7 '05 #4
On Wed, 7 Dec 2005 17:43:41 +0000, Alan Silver
<al*********@no spam.thanx> wrote:

I tried that one already, and got an "Object reference not set to an
instance of an object" exception on the last line. I have no idea why as
the web.config file is being loaded (the other way of doing it got the
attribute value fine), so I don't know why this doesn't work.

Any ideas? Thanks.


Make sure the web.config file doesn't specify a default namespace, i.e
if the following is present in the <configuratio n> element:

xmlns="http://schemas.microso ft.com/.NetConfigurati on/v2.0"

then the XPath you are using will not find any nodes.

You can either remove the default namespace, or you'll have to use an
XmlNamespaceMan ager with SelectSingleNod e.

--
Scott
http://www.OdeToCode.com/blogs/scott/

Dec 8 '05 #5
>>I tried that one already, and got an "Object reference not set to an
instance of an object" exception on the last line. I have no idea why as
the web.config file is being loaded (the other way of doing it got the
attribute value fine), so I don't know why this doesn't work.

Any ideas? Thanks.


Make sure the web.config file doesn't specify a default namespace, i.e
if the following is present in the <configuratio n> element:

xmlns="http://schemas.microso ft.com/.NetConfigurati on/v2.0"

then the XPath you are using will not find any nodes.

You can either remove the default namespace, or you'll have to use an
XmlNamespaceMa nager with SelectSingleNod e.


You're a genius!!!

That was it. I removed it and it worked fine.

OK, for the XML-newbie (ie me), please explain the purpose of the
namespace, why it was there and why I don't need it. If everything works
fine without it, why have it.

Thanks very much.

--
Alan Silver
(anything added below this line is nothing to do with me)
Dec 8 '05 #6
Alan,

Namespaces are intended to define the syntax (collection of nodes and
their relationships) within a single XML document. The namespace is
tied to a URI, which is supposed to be unique and can be used to
further distinguish one XML document from another.

The reason that this is important, is that multiple XML documents can
make up a larger document - or a single XML syntax can be split into a
more granular sytax, based upon your requirements.

Here is a practical example: SOAP. SOAP has a namespace that
identifies the syntax of the SOAP XML. It is possible to add your own
XML syntax within the parts of the SOAP request/response model. By
specifying a namespace for your syntax, in addition to the one
specified for SOAP, you can apply schema validation on a per namespace
basis, or select nodes only within a specific namespace.

Taking that last sentence into consideration. Assume that you have two
distinctly different XML syntaxes. One is for a "person", containing
things such as name, age, sex, etc. Now, consider you also have a
syntax for a "pet". The pet shares some of the same syntax elements.
If you use them in a third XML syntax, say "family" (yes, my pets are
part of my family), assigning namespaces for the person and pet
syntaxes will allow you to distinguish between a person's name and a
pet's name, for example.

HTH,

Joseph

Dec 8 '05 #7
On Thu, 8 Dec 2005 16:09:42 +0000, Alan Silver
<al*********@no spam.thanx> wrote:

OK, for the XML-newbie (ie me), please explain the purpose of the
namespace, why it was there and why I don't need it. If everything works
fine without it, why have it.


Joseph gave you a real good overview of namespaces in general.

In a 2.0 web.config file you don't need the namespace for the file to
operate correctly - everything still works at runtime, and
unfortunately the presence of the namespace does break stuff -
including intellisense in the IDE when editing the file. The ASP.NET
Web Site Configuration tool puts the namespace in and most people will
take it out afterwards to get intellisense working.

--
Scott
http://www.OdeToCode.com/blogs/scott/

Dec 8 '05 #8
Joseph,

Thanks for the explanation, that makes it a lot clearer.
Alan,

Namespaces are intended to define the syntax (collection of nodes and
their relationships) within a single XML document. The namespace is
tied to a URI, which is supposed to be unique and can be used to
further distinguish one XML document from another.

The reason that this is important, is that multiple XML documents can
make up a larger document - or a single XML syntax can be split into a
more granular sytax, based upon your requirements.

Here is a practical example: SOAP. SOAP has a namespace that
identifies the syntax of the SOAP XML. It is possible to add your own
XML syntax within the parts of the SOAP request/response model. By
specifying a namespace for your syntax, in addition to the one
specified for SOAP, you can apply schema validation on a per namespace
basis, or select nodes only within a specific namespace.

Taking that last sentence into consideration. Assume that you have two
distinctly different XML syntaxes. One is for a "person", containing
things such as name, age, sex, etc. Now, consider you also have a
syntax for a "pet". The pet shares some of the same syntax elements.
If you use them in a third XML syntax, say "family" (yes, my pets are
part of my family), assigning namespaces for the person and pet
syntaxes will allow you to distinguish between a person's name and a
pet's name, for example.

HTH,

Joseph


--
Alan Silver
(anything added below this line is nothing to do with me)
Dec 8 '05 #9

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

Similar topics

22
3023
by: Daniel Billingsley | last post by:
Ok, I wanted to ask this separate from nospam's ridiculous thread in hopes it could get some honest attention. VB6 had a some simple and fast mechanisms for retrieving values from basic text files, which in turn could be simply and easily maintained with notepad. I understand the benefits of XML, really, but in the case of configuration files it seems it is almost always nothing more than unnecessary complexity, both in accessing them...
7
6580
by: | last post by:
In the beginning we had Ini files. Later we had registery files. Now have xml files and our read-only myapp.config file. My question now, is what is the best way to store and load user and machine specific settings for a .NET program? And what classes, or code do we have to do this in C#? I don't think that using the myapp.config is the best choice to store since the file might be read-only if the program is started from a CD ROM, or...
1
5363
by: vkrasner | last post by:
It works with VS2003 and does not in VS2005: in VS2003 : string sMyvalue = ConfigurationSettings.AppSettings; in VS2005 (does not work!!) string sMyvalue = ConfigurationManager.AppSettings; Anybody able to give me idea how-to read by C# element which I add to the machine.config into the new single section?
12
2896
by: James Norton-Jones | last post by:
Hi, Am I trying to hold the data of a DataGrid in a label so that when the form is reposted the DataGrid can be repopulated. The problem I am having is that I don't understand how to get the text into a stream in order to be able to use DataSetOutcomes1.ReadXML(MyStream). Thanks in advance, James
1
3283
by: Christian Rühl | last post by:
hey! what i wanna do sounds very simple at first, but it turned out to be a real bone crusher... i want to check if a treeView node is checked and if a correspondent node in my xml config file exists just to sort of synchronize them by changing the xml nodes attribute(s). somehow i always catch an exception "blabla has an invalid token" but i cannot find a solution for this. maybe someone of you people can tell me how to do this...
1
2605
by: Christian Rühl | last post by:
hey! what i wanna do sounds very simple at first, but it turned out to be a real bone crusher... i want to check if a treeView node is checked and if a correspondent node in my xml config file exists just to sort of synchronize them by changing the xml nodes attribute(s). somehow i always catch an exception "blabla has an invalid token" but i cannot find a solution for this. maybe someone of you people can tell me how to do this...
3
2143
by: gordon | last post by:
Hi I am looking to store some details about a user's configuration choices, in particular the place where they have installed some data files, the OS that they use, and their Windows user name. This information is used in a windows C#.net application. I would like to capture this info the first time that the user opens the app, but each subsequent time to make sure that the location is current when they open the application. This is...
6
7301
by: J.Marsch | last post by:
I must be completely losing my mind. I have some code that writes to config files. It works great with app.config files, but fails miserably with web.config files. For the life of me, I cannot figure out what is going on here. I have taken it all the way back to just selecting the configuration node (top level node), and it fails! How can this line fail (returns null)??
4
2769
by: ink | last post by:
Hi all, I am trying to pull some financial data off of an HTML web page so that I can store it in a Database for Sorting and filtering. I have been thinking about this for some time and trying to find the best way to do it but I am just not experienced enough with this sort of thing to make the best decision, so any advice would be great. The data is on a number of different nested tables with in the HTML, and on a number of different...
0
9795
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10498
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10540
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10212
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9319
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6951
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5789
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3970
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3077
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.