1

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.

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0
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.

3 Comments

The Application.StartupPath() property is specific to WinForms, no?
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.
Good call! I forgot that Server.MapPath was webforms inclusive.
0

If in SSIS then try this:

'Loading an Xml File from VB'

 Dim xmlDoc As XmlDocument = New XmlDocument
 xmlDoc.Load("C:\Test\sample.xml")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.