简体   繁体   中英

How to load an XML document using a relative path in VB.net?

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.

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.

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.

If in SSIS then try this:

'Loading an Xml File from VB'

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM