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

A

Alan Silver

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 SelectSingleNode 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...

XmlAttributeCollection pagesAttributes = webConfig.FirstChild.NextSibling.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.
 
K

Karl Seguin

This works fine for me:
XmlDocument document = new XmlDocument();
document.Load(Server.MapPath("~/web.config"));
XmlNode node =
document.SelectSingleNode("/configuration/system.web/pages");
string theme = node.Attributes["theme"].Value;

Karl
 
A

Alan Silver

This works fine for me:
XmlDocument document = new XmlDocument();
document.Load(Server.MapPath("~/web.config"));
XmlNode node =
document.SelectSingleNode("/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.
 
K

Karl Seguin

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 said:
This works fine for me:
XmlDocument document = new XmlDocument();
document.Load(Server.MapPath("~/web.config"));
XmlNode node =
document.SelectSingleNode("/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.
 
S

Scott Allen

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 <configuration> element:

xmlns="http://schemas.microsoft.com/.NetConfiguration/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
XmlNamespaceManager with SelectSingleNode.
 
A

Alan Silver

I tried that one already, and got an "Object reference not set to an
Make sure the web.config file doesn't specify a default namespace, i.e
if the following is present in the <configuration> element:

xmlns="http://schemas.microsoft.com/.NetConfiguration/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
XmlNamespaceManager with SelectSingleNode.

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.
 
J

Joseph Ferris

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
 
S

Scott Allen

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.
 

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,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top