tags:

views:

1110

answers:

2

In VB.net when loading in XML documents using System.Xml.Xmldocument is there a way that I can specify a relative path to the file?

path = "file.xml"
xmld.Load(path)

The XML doc I'm trying to load is in the same directory as the VB class. But I'm having trouble accessing it without using a full path to the XML doc.

A: 

Import only System.Xml and try...

Dim xmlDoc As XmlDocument = New XmlDocument
 xmlDoc.Load(Server.MapPath("Divide.xml"))

Divide.xml will obviously be replaced by your xml file's name.
From MSDN, Server.MapPath is as follows..

Specifies the relative or virtual path to map to a physical directory. If Path starts with either a forward (/) or backward slash (), the MapPath method returns a path as if Path were a full, virtual path. If Path doesn't start with a slash, the MapPath method returns a path relative to the directory of the .asp file being processed.

Chris
A: 
Application.StartupPath()

will point to the executing location of the application. If the final build location for your XML file will be in a directory different than this, I recommend creating a small file manager class that will point to the proper location of the file. That way you can simply call:

xmlDoc.Load(myFileMan.FilePath())

and let the manager resolve the proper path based on a debug/release build and any other potential mitigating factors on it.

Dillie-O
The Application.StartupPath() property is specific to WinForms, no?
Chris
True, just as Server.MapPath is specific to WebForms. Your question really didn't indicate which type of app you were using, so I wanted to toss the alternative in there to let you know.
Dillie-O
Good call! I forgot that Server.MapPath was webforms inclusive.
Chris