473,595 Members | 2,474 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

reading XML files from codebehind file

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>thi sIsTheNameOfADa tabaseServer</ServerName>
</aConfigFile>

--
Martin Eyles
ma**********@NO SPAM.bytronic.c om

Nov 19 '05 #1
8 5758
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>thi sIsTheNameOfADa tabaseServer</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(MapPat h("myfile.confi g");

XmlNode nd = cfg.SelectSingl eNode("//ServerName");

but: you either have to remove the namespace (xmlns=..), or figure out how to
use the overload of SelectSingleNod e with namespacemanage r.

Hans Kesting

Nov 19 '05 #2
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.XmlD ataDocument cfg = new System.Xml.XmlD ataDocument();
cfg.Load(MapPat h("aConfigFile. config"));
System.Xml.XmlN ode nd = cfg.SelectSingl eNode("//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.XmlN ode object. Any Ideas?

Thanks,
Martin

--
Martin Eyles
ma**********@NO SPAM.bytronic.c om

"Hans Kesting" <ne***********@ spamgourmet.com > wrote in message
news:em******** ******@TK2MSFTN GP09.phx.gbl...
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>thi sIsTheNameOfADa tabaseServer</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(MapPat h("myfile.confi g");

XmlNode nd = cfg.SelectSingl eNode("//ServerName");

but: you either have to remove the namespace (xmlns=..), or figure out how to use the overload of SelectSingleNod e with namespacemanage r.

Hans Kesting

Nov 19 '05 #3
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.XmlD ataDocument
cfg.Load(MapPat h("aConfigFile. config"))
Dim nd As System.Xml.XmlN ode
nd = cfg.SelectSingl eNode("//ServerName")
Response.Write( nd.InnerText)

--
Martin Eyles
ma**********@NO SPAM.bytronic.c om

"Martin Eyles" <ma**********@N OSPAM.bytronic. com> wrote in message
news:11******** *****@corp.supe rnews.com...
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.XmlD ataDocument cfg = new System.Xml.XmlD ataDocument();
cfg.Load(MapPat h("aConfigFile. config"));
System.Xml.XmlN ode nd = cfg.SelectSingl eNode("//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.XmlN ode object. Any Ideas?

Thanks,
Martin

Nov 19 '05 #4
Is there some reason that you are not using
System.Configur ationSettings.A ppSettings() 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
TeleCommunicati on Systems, Inc.

Nov 19 '05 #5
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
ma**********@NO SPAM.bytronic.c om

"Peter Kryszak" <Pe**********@d iscussions.micr osoft.com> wrote in message
news:98******** *************** ***********@mic rosoft.com...
Is there some reason that you are not using
System.Configur ationSettings.A ppSettings() 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
TeleCommunicati on Systems, Inc.

Nov 19 '05 #6
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" <ma**********@N OSPAM.bytronic. com> wrote in message
news:11******** *****@corp.supe rnews.com...
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
ma**********@NO SPAM.bytronic.c om

"Peter Kryszak" <Pe**********@d iscussions.micr osoft.com> wrote in message
news:98******** *************** ***********@mic rosoft.com...
Is there some reason that you are not using
System.Configur ationSettings.A ppSettings() 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
TeleCommunicati on Systems, Inc.


Nov 19 '05 #7
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>ser ver</ServerName>
</L_______Config>

(L_______ is just to obscure a product name)

Thanks,
Martin

--
Martin Eyles
ma**********@NO SPAM.bytronic.c om

"Kevin Spencer" <ke***@DIESPAMM ERSDIEtakempis. com> wrote in message
news:el******** *****@TK2MSFTNG P12.phx.gbl...
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" <ma**********@N OSPAM.bytronic. com> wrote in message
news:11******** *****@corp.supe rnews.com...
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
ma**********@NO SPAM.bytronic.c om

"Peter Kryszak" <Pe**********@d iscussions.micr osoft.com> wrote in message news:98******** *************** ***********@mic rosoft.com...
Is there some reason that you are not using
System.Configur ationSettings.A ppSettings() 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
TeleCommunicati on Systems, Inc.



Nov 19 '05 #8
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.XmlD ocument 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" <ma**********@N OSPAM.bytronic. com> wrote in message
news:11******** *****@corp.supe rnews.com...
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>ser ver</ServerName>
</L_______Config>

(L_______ is just to obscure a product name)

Thanks,
Martin

--
Martin Eyles
ma**********@NO SPAM.bytronic.c om

"Kevin Spencer" <ke***@DIESPAMM ERSDIEtakempis. com> wrote in message
news:el******** *****@TK2MSFTNG P12.phx.gbl...
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" <ma**********@N OSPAM.bytronic. com> wrote in message
news:11******** *****@corp.supe rnews.com...
> 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
> ma**********@NO SPAM.bytronic.c om
>
> "Peter Kryszak" <Pe**********@d iscussions.micr osoft.com> wrote in message > news:98******** *************** ***********@mic rosoft.com...
>> Is there some reason that you are not using
>> System.Configur ationSettings.A ppSettings() 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
>> TeleCommunicati on Systems, Inc.
>>
>
>



Nov 19 '05 #9

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

Similar topics

1
3299
by: Roberto Castro | last post by:
I have some problems with the way I am showing the BLOB fields in the Image web controls. It does work on my localhost though sometimes I need to hit Refresh for the images to load properly. However, when I try to access the same page from another machine or when I test the code on a remote server the images just wouldn't show. The page I am talking about has 3 Image web controls. In the CodeBehind I am loading these controls by using an...
6
1845
by: Maurice Walmsley | last post by:
hi, I hope someone can help me with this please I'm trying to move my codebehind files to a folder other than the one my webform sits in. I've tried changing the 'scr' page level attribute of my webform, but i get the following parser error; Parser Error Message: 'c:\WebForm2.aspx.vb' is a physical path. this the code;
12
1743
by: A.M | last post by:
Hi, Using VS.NET 2003, If i use SRC page attribute instead of CodeBehind, do i still have intelisence and generally IDE support for that? Thanks, Ali
4
2203
by: Chris | last post by:
I work on a development server (Server A), and have a staging server (Server B), where updated ASP.Net files from Server A are sent to. I noticed that Server B will reflect changes made to .aspx files just by copying the new files from Server A. Server B only has the framework installed, and the project folder structure is enormous (100MB+). I know that I can rebuild the project on Server A, which has .Net Studio, and copy the new...
3
3339
by: Dan | last post by:
Hi, I have a problem using an aspx page with a Control on it. I get the following error message Compiler Error Message: CS1595: 'Test.Class2' is defined in multiple places; using definition from 'c:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\Temporary ASP.NET Files\root\1f575646\ad3a161b\assembly\dl2\57ca505e\044565c0_f84fc401\Test1.DLL' The problem is that the control is defined in two different assemblies
6
2008
by: Thomas Andersson | last post by:
Hi all, I've been thinking of about adding new languages to our website and is currently investigating how this could be done. And I know that one way to go would be to create new aspx-pages with new codebehinds for each page I want to have translated. But my question is - is it ok to keep the same codebehind file for a certain page which is translated into different languages. In this way I only need to change to aspx-file (html) of...
2
935
by: Arsalan | last post by:
How do I put different files in different folders in ASP.NET project using VS Studio [without loosing the functionality ? For e.g, i want all .aspx files to be under \page folder and all .vb files in \vb folder ?
7
2229
by: Matt Jensen | last post by:
Howdy I want to simulate with .Net what I used to do with classic ASP where you would have a series of include files with utility functions that you would include when you needed them. I read some article about creating a utility class, but it gave no details, and I'm not sure what to do. Do I create a .cs file and then just include it? What should the class inherit from? And how do you inherit more than one class on your page in...
8
2041
by: vinesh | last post by:
I have sample Asp.Net Web Application project. Let me know how to keep the files related to this project (like the webform.aspx, WebForm1.aspx.vb, WebForm1.aspx.resx) in a separate folder within a project and what all setting will I be required to do So that my original project works. Can I keep WebForm1.aspx in root and code behind file in a folder within the project folder? Thanks in advance.
0
8261
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8019
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
6674
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
5418
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
3873
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3911
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2391
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1490
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1223
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.