reading XML files from codebehind file

Discussion in 'ASP .Net' started by Martin Eyles, Jun 27, 2005.

  1. Martin Eyles

    Martin Eyles Guest

    Hi,
    I have a configuration file I made in xml which I am using to name a
    database server. (this way I can deploy my web page on various servers, and
    just change this file to make it work). Unfortunately, I can't figure out
    how to read this from the server side code behind file (either vb or c#).
    Can anyone
    post some sample code for this.

    Thanks,
    Martin

    (xml file follows)

    aConfigFile.xml
    -------------------
    <?xml version="1.0" encoding="utf-8" ?>
    <aConfigFile xmlns="http://tempuri.org/aConfigFile.xsd">
    <ServerName>thisIsTheNameOfADatabaseServer</ServerName>
    </aConfigFile>

    --
    Martin Eyles
     
    Martin Eyles, Jun 27, 2005
    #1
    1. Advertising

  2. Martin Eyles

    Hans Kesting Guest

    Martin Eyles wrote:
    > Hi,
    > I have a configuration file I made in xml which I am using to name
    > a database server. (this way I can deploy my web page on various
    > servers, and just change this file to make it work). Unfortunately, I
    > can't figure out how to read this from the server side code behind
    > file (either vb or c#). Can anyone
    > post some sample code for this.
    >
    > Thanks,
    > Martin
    >
    > (xml file follows)
    >
    > aConfigFile.xml
    > -------------------
    > <?xml version="1.0" encoding="utf-8" ?>
    > <aConfigFile xmlns="http://tempuri.org/aConfigFile.xsd">
    > <ServerName>thisIsTheNameOfADatabaseServer</ServerName>
    > </aConfigFile>


    You have to figure out the exact path to your file (hint: use MapPath),
    then you can load that into an XmlDocument. If you place this file
    within the website, use the extension ".config" (instead of .xml), then visitors
    can't access that file.

    XmlDocument cfg = new XmlDocument();
    cfg.Load(MapPath("myfile.config");

    XmlNode nd = cfg.SelectSingleNode("//ServerName");

    but: you either have to remove the namespace (xmlns=..), or figure out how to
    use the overload of SelectSingleNode with namespacemanager.

    Hans Kesting
     
    Hans Kesting, Jun 27, 2005
    #2
    1. Advertising

  3. Martin Eyles

    Martin Eyles Guest

    Hi,
    Thanks for the code. I had to change XmlDocument to XmlDataDocument, but
    otherwise it works (from a c# page). This is the final version I have put in
    the test page I did.

    System.Xml.XmlDataDocument cfg = new System.Xml.XmlDataDocument();
    cfg.Load(MapPath("aConfigFile.config"));
    System.Xml.XmlNode nd = cfg.SelectSingleNode("//ServerName");
    Response.Write(nd.InnerText);

    I just need to know what to do to convert it to VB.NET, as some of my pages
    use this too. Most of this is Ok - My problem is that vb doesn't appear to
    support the System.Xml.XmlNode object. Any Ideas?

    Thanks,
    Martin

    --
    Martin Eyles


    "Hans Kesting" <> wrote in message
    news:...
    > Martin Eyles wrote:
    > > Hi,
    > > I have a configuration file I made in xml which I am using to name
    > > a database server. (this way I can deploy my web page on various
    > > servers, and just change this file to make it work). Unfortunately, I
    > > can't figure out how to read this from the server side code behind
    > > file (either vb or c#). Can anyone
    > > post some sample code for this.
    > >
    > > Thanks,
    > > Martin
    > >
    > > (xml file follows)
    > >
    > > aConfigFile.xml
    > > -------------------
    > > <?xml version="1.0" encoding="utf-8" ?>
    > > <aConfigFile xmlns="http://tempuri.org/aConfigFile.xsd">
    > > <ServerName>thisIsTheNameOfADatabaseServer</ServerName>
    > > </aConfigFile>

    >
    > You have to figure out the exact path to your file (hint: use MapPath),
    > then you can load that into an XmlDocument. If you place this file
    > within the website, use the extension ".config" (instead of .xml), then

    visitors
    > can't access that file.
    >
    > XmlDocument cfg = new XmlDocument();
    > cfg.Load(MapPath("myfile.config");
    >
    > XmlNode nd = cfg.SelectSingleNode("//ServerName");
    >
    > but: you either have to remove the namespace (xmlns=..), or figure out how

    to
    > use the overload of SelectSingleNode with namespacemanager.
    >
    > Hans Kesting
    >
    >
    >
     
    Martin Eyles, Jun 27, 2005
    #3
  4. Martin Eyles

    Martin Eyles Guest

    Sorry,
    just discovered what was wrong with the conversion. intelitext kept
    trying to make it XmlNodeType, but higher up the list XmlNode was available,
    and this works. The VB code works out to be:-

    Dim cfg As New System.Xml.XmlDataDocument
    cfg.Load(MapPath("aConfigFile.config"))
    Dim nd As System.Xml.XmlNode
    nd = cfg.SelectSingleNode("//ServerName")
    Response.Write(nd.InnerText)

    --
    Martin Eyles


    "Martin Eyles" <> wrote in message
    news:...
    > Hi,
    > Thanks for the code. I had to change XmlDocument to XmlDataDocument,

    but
    > otherwise it works (from a c# page). This is the final version I have put

    in
    > the test page I did.
    >
    > System.Xml.XmlDataDocument cfg = new System.Xml.XmlDataDocument();
    > cfg.Load(MapPath("aConfigFile.config"));
    > System.Xml.XmlNode nd = cfg.SelectSingleNode("//ServerName");
    > Response.Write(nd.InnerText);
    >
    > I just need to know what to do to convert it to VB.NET, as some of my

    pages
    > use this too. Most of this is Ok - My problem is that vb doesn't appear to
    > support the System.Xml.XmlNode object. Any Ideas?
    >
    > Thanks,
    > Martin
     
    Martin Eyles, Jun 27, 2005
    #4
  5. Is there some reason that you are not using
    System.ConfigurationSettings.AppSettings() to get to the web.config file?

    This is a very easy way for ASP.NET code (C# or VB.NET) to get to
    configuration data.

    --
    Peter Kryszak
    TeleCommunication Systems, Inc.
     
    =?Utf-8?B?UGV0ZXIgS3J5c3phaw==?=, Sep 9, 2005
    #5
  6. Martin Eyles

    Martin Eyles Guest

    web.config has loads of junk data that I don't want in my config file. I
    want an easy to write config file that I can write for each of the places
    this is installed.

    --
    Martin Eyles


    "Peter Kryszak" <> wrote in message
    news:...
    > Is there some reason that you are not using
    > System.ConfigurationSettings.AppSettings() to get to the web.config file?
    >
    > This is a very easy way for ASP.NET code (C# or VB.NET) to get to
    > configuration data.
    >
    > --
    > Peter Kryszak
    > TeleCommunication Systems, Inc.
    >
     
    Martin Eyles, Sep 13, 2005
    #6
  7. Well, Martin, I would recommend using the .Net Configuration namespace, and
    the web.config file. It may be uncomfortable to you now, but it is very
    well-conceived, extensible, and standardized, meaning that other .Net
    developers will already know how to use it if necessary.

    However, assuming you want to go this route, check out the various
    System.Xml namespaces. The .Net platform also has XML support out the wazoo.
    Which namespaces and classes you use depend on how you want to use the XML.
    Do you want a simple XML document, or something more strongly-typed? Do you
    need to work with a DTD? A Schema? Again, the .Net platform and CLR support
    all of the XML specification.

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    Neither a follower nor a lender be.

    "Martin Eyles" <> wrote in message
    news:...
    > web.config has loads of junk data that I don't want in my config file. I
    > want an easy to write config file that I can write for each of the places
    > this is installed.
    >
    > --
    > Martin Eyles
    >
    >
    > "Peter Kryszak" <> wrote in message
    > news:...
    >> Is there some reason that you are not using
    >> System.ConfigurationSettings.AppSettings() to get to the web.config file?
    >>
    >> This is a very easy way for ASP.NET code (C# or VB.NET) to get to
    >> configuration data.
    >>
    >> --
    >> Peter Kryszak
    >> TeleCommunication Systems, Inc.
    >>

    >
    >
     
    Kevin Spencer, Sep 13, 2005
    #7
  8. Martin Eyles

    Martin Eyles Guest

    for context, the file only contains the name of an sql server at the moment.
    There may be a couple of added bits later, but probably not more than five
    tags. The config file works now, so I'm not bothered about any extra. Just
    want to keep it simple. (thanks for all the earlier help, this sorted out
    the problems I was having). Still, I take the point that, if I'm doing
    something more complex, it might be useful to use another method.

    <?xml version="1.0" encoding="utf-8" ?>
    <L_______Config>
    <ServerName>server</ServerName>
    </L_______Config>

    (L_______ is just to obscure a product name)

    Thanks,
    Martin

    --
    Martin Eyles


    "Kevin Spencer" <> wrote in message
    news:...
    > Well, Martin, I would recommend using the .Net Configuration namespace,

    and
    > the web.config file. It may be uncomfortable to you now, but it is very
    > well-conceived, extensible, and standardized, meaning that other .Net
    > developers will already know how to use it if necessary.
    >
    > However, assuming you want to go this route, check out the various
    > System.Xml namespaces. The .Net platform also has XML support out the

    wazoo.
    > Which namespaces and classes you use depend on how you want to use the

    XML.
    > Do you want a simple XML document, or something more strongly-typed? Do

    you
    > need to work with a DTD? A Schema? Again, the .Net platform and CLR

    support
    > all of the XML specification.
    >
    > --
    > HTH,
    >
    > Kevin Spencer
    > Microsoft MVP
    > .Net Developer
    > Neither a follower nor a lender be.
    >
    > "Martin Eyles" <> wrote in message
    > news:...
    > > web.config has loads of junk data that I don't want in my config file. I
    > > want an easy to write config file that I can write for each of the

    places
    > > this is installed.
    > >
    > > --
    > > Martin Eyles
    > >
    > >
    > > "Peter Kryszak" <> wrote in

    message
    > > news:...
    > >> Is there some reason that you are not using
    > >> System.ConfigurationSettings.AppSettings() to get to the web.config

    file?
    > >>
    > >> This is a very easy way for ASP.NET code (C# or VB.NET) to get to
    > >> configuration data.
    > >>
    > >> --
    > >> Peter Kryszak
    > >> TeleCommunication Systems, Inc.
    > >>

    > >
    > >

    >
    >
     
    Martin Eyles, Sep 13, 2005
    #8
  9. Well, Martin, you actually have a lot of control over your web.config file
    (and other configuration files). You can add custom configuration sections,
    use the existing appSettings section, and get quite a lot of mileage out of
    it, without getting too complicated. You can also add comments to it.

    But for a simple XML file such as you've described, the
    System.Xml.XmlDocument class can provide you with most of what you need. You
    can create one by simply passing a path to the XML file to its constructor.
    And you can iterate through its nodes, and plenty more, with a great deal of
    detail.

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    Neither a follower nor a lender be.

    "Martin Eyles" <> wrote in message
    news:...
    > for context, the file only contains the name of an sql server at the
    > moment.
    > There may be a couple of added bits later, but probably not more than five
    > tags. The config file works now, so I'm not bothered about any extra. Just
    > want to keep it simple. (thanks for all the earlier help, this sorted out
    > the problems I was having). Still, I take the point that, if I'm doing
    > something more complex, it might be useful to use another method.
    >
    > <?xml version="1.0" encoding="utf-8" ?>
    > <L_______Config>
    > <ServerName>server</ServerName>
    > </L_______Config>
    >
    > (L_______ is just to obscure a product name)
    >
    > Thanks,
    > Martin
    >
    > --
    > Martin Eyles
    >
    >
    > "Kevin Spencer" <> wrote in message
    > news:...
    >> Well, Martin, I would recommend using the .Net Configuration namespace,

    > and
    >> the web.config file. It may be uncomfortable to you now, but it is very
    >> well-conceived, extensible, and standardized, meaning that other .Net
    >> developers will already know how to use it if necessary.
    >>
    >> However, assuming you want to go this route, check out the various
    >> System.Xml namespaces. The .Net platform also has XML support out the

    > wazoo.
    >> Which namespaces and classes you use depend on how you want to use the

    > XML.
    >> Do you want a simple XML document, or something more strongly-typed? Do

    > you
    >> need to work with a DTD? A Schema? Again, the .Net platform and CLR

    > support
    >> all of the XML specification.
    >>
    >> --
    >> HTH,
    >>
    >> Kevin Spencer
    >> Microsoft MVP
    >> .Net Developer
    >> Neither a follower nor a lender be.
    >>
    >> "Martin Eyles" <> wrote in message
    >> news:...
    >> > web.config has loads of junk data that I don't want in my config file.
    >> > I
    >> > want an easy to write config file that I can write for each of the

    > places
    >> > this is installed.
    >> >
    >> > --
    >> > Martin Eyles
    >> >
    >> >
    >> > "Peter Kryszak" <> wrote in

    > message
    >> > news:...
    >> >> Is there some reason that you are not using
    >> >> System.ConfigurationSettings.AppSettings() to get to the web.config

    > file?
    >> >>
    >> >> This is a very easy way for ASP.NET code (C# or VB.NET) to get to
    >> >> configuration data.
    >> >>
    >> >> --
    >> >> Peter Kryszak
    >> >> TeleCommunication Systems, Inc.
    >> >>
    >> >
    >> >

    >>
    >>

    >
    >
     
    Kevin Spencer, Sep 13, 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. John Spiegel

    Newbie: Multiple codebehind files

    John Spiegel, Aug 27, 2003, in forum: ASP .Net
    Replies:
    5
    Views:
    6,151
    bboland1
    Aug 30, 2007
  2. Scott Allen

    Re: reusing codebehind files?

    Scott Allen, Apr 13, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    366
    Anders Bohlin
    Apr 13, 2004
  3. Replies:
    0
    Views:
    807
  4. Erik Wasser
    Replies:
    5
    Views:
    529
    Peter J. Holzer
    Mar 5, 2006
  5. Replies:
    5
    Views:
    111
    Chris Angelico
    May 14, 2014
Loading...

Share This Page