server.mappath

Bryan

Freshman
Joined
Jun 17, 2002
Messages
49
Location
Cedar Hill, TX
Ok, I am having trouble accessing an xml file.
I have a control that uses a mainMenu.xml file to get the menu structure for a menu. You have to give the control the data source and then bind the data. Well I have this menu on several pages and right now I have to have the mainMenu.xml file in every folder that has a page that access the xml file.

What I want to be able to do is keep 1 xml file in my root directory http://localhost/ and not one in each directory.

When I set the datasource property to this xml file, what do I need to put for the path? when I use server.mapPath I get something similar to c:\inetpub\wwwroot\AxisFellowship\sermons\lotr\mainMenu.xml

The thing is I don't want to have to change the property on each page that uses the menu so it will work once i upload it to my web server.

can I use something like

Menu1.DataSource = "./mainMenu.xml"

Or is there some other way to do this?
 
If you were to put the xml file in the root directory:
http://localhost/mainMenu.xml

Then if you had pages in
http://localhost/Folder1/

Menu1.DataSource would then = "../mainMenu.xml"

Then if you had pages in
http://localhost/Folder1/Folder2/

Menu1.DataSource would then = "../../mainMenu.xml"

Some interesting combinations of folders get a bit trickier, but if you maintain these virtual paths, then moving your pages around stays nice and easy.

Hope this answered your question.
 
Couldn't you store the correct path to the file in the web.config - the control could then read this at runtime. When moving from development to production you would just need to update this one file.
 
PlausiblyDamp said:
Couldn't you store the correct path to the file in the web.config - the control could then read this at runtime. When moving from development to production you would just need to update this one file.
that sounds like a good idea, but I must admit that I am still learning ASP.net and I don't know how to do that. Could you show me the code I would need to include in the web.config to do this?

Thanks
Bryan
 
given the following web.config
Code:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>
        <add key="PathToFile" value="<enter file path here>" />
    </appSettings>
</configuration>
you shouold be able to read it with code similar to
Visual Basic:
Dim FilePath As String = ConfigurationSettings.AppSettings("PathToFile")

not tested this as I don't have VS installed on this machine but it should give you the idea.
 
ok, I did that, but now I am getting this error:

c:\inetpub\wwwroot\AxisFellowship\default.aspx.vb(26): Value of type 'String' cannot be converted to '1-dimensional array of String'.

any ideas?

Thanks,
Bryan
 
PlausiblyDamp said:
given the following web.config
Code:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>
        <add key="PathToFile" value="<enter file path here>" />
    </appSettings>
</configuration>
you shouold be able to read it with code similar to
Visual Basic:
Dim FilePath As String = ConfigurationSettings.AppSettings("PathToFile")

not tested this as I don't have VS installed on this machine but it should give you the idea.
ok, so I went a slightly different route. instead of the vb code you mentioned I did this:
Code:
Menu1.DataSource = ConfigurationSettings.AppSettings("MainMenuXMLPath")
and it works fine, I don't get the error I mentioned above.
 
Did you accidently declare your variable as
Visual Basic:
dim FilePath as String()
'or
Dim FilePath() as string
the top one always catches me out - as it declares an array of strings not a single string.
 
PlausiblyDamp said:
Did you accidently declare your variable as
Visual Basic:
dim FilePath as String()
'or
Dim FilePath() as string
the top one always catches me out - as it declares an array of strings not a single string.
that would be the problem.
 
Back
Top