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

Discussion in 'ASP .Net' started by Alan Silver, Dec 7, 2005.

  1. Alan Silver

    Alan Silver Guest

    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.

    --
    Alan Silver
    (anything added below this line is nothing to do with me)
     
    Alan Silver, Dec 7, 2005
    #1
    1. Advertising

  2. Alan Silver

    Karl Seguin Guest

    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

    --
    MY ASP.Net tutorials
    http://www.openmymind.net/


    "Alan Silver" <> wrote in message
    news:hE2yy$...
    > 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.
    >
    > --
    > Alan Silver
    > (anything added below this line is nothing to do with me)
     
    Karl Seguin, Dec 7, 2005
    #2
    1. Advertising

  3. Alan Silver

    Alan Silver Guest

    <karl@REMOVE.?.invalid> writes
    >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.

    --
    Alan Silver
    (anything added below this line is nothing to do with me)
     
    Alan Silver, Dec 7, 2005
    #3
  4. Alan Silver

    Karl Seguin Guest

    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" <> wrote in message
    news:...
    > <karl@REMOVE.?.invalid> writes
    >>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.
    >
    > --
    > Alan Silver
    > (anything added below this line is nothing to do with me)
     
    Karl Seguin, Dec 7, 2005
    #4
  5. Alan Silver

    Scott Allen Guest

    On Wed, 7 Dec 2005 17:43:41 +0000, Alan Silver
    <> 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 <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.

    --
    Scott
    http://www.OdeToCode.com/blogs/scott/
     
    Scott Allen, Dec 8, 2005
    #5
  6. Alan Silver

    Alan Silver Guest

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


    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)
     
    Alan Silver, Dec 8, 2005
    #6
  7. 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
     
    Joseph Ferris, Dec 8, 2005
    #7
  8. Alan Silver

    Alan Silver Guest

    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)
     
    Alan Silver, Dec 8, 2005
    #8
  9. Alan Silver

    Scott Allen Guest

    On Thu, 8 Dec 2005 16:09:42 +0000, Alan Silver
    <> 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/
     
    Scott Allen, Dec 8, 2005
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mike John
    Replies:
    2
    Views:
    366
    Ray Dixon [MVP]
    Jul 23, 2003
  2. =?Utf-8?B?RXNraW1v?=

    C# Databinder.Eval didn't work quite right...

    =?Utf-8?B?RXNraW1v?=, Nov 5, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    3,476
    Scott Allen
    Nov 7, 2004
  3. DC Gringo
    Replies:
    2
    Views:
    6,427
    DC Gringo
    Nov 18, 2004
  4. Replies:
    5
    Views:
    294
    Vinay Sajip
    Jan 19, 2007
  5. CSharpner
    Replies:
    0
    Views:
    1,048
    CSharpner
    Apr 9, 2007
Loading...

Share This Page