Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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?

Posted

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.

Posted
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

  • Administrators
Posted

given the following web.config



   
       
   

you shouold be able to read it with code similar to

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.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

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

Posted
given the following web.config

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

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:

Menu1.DataSource = ConfigurationSettings.AppSettings("MainMenuXMLPath")

and it works fine, I don't get the error I mentioned above.

Posted
Did you accidently declare your variable as

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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...