LenseOnLife
LenseOnLife

Reputation: 179

How do I access an XML file that is included in the project?

I have added an XML file (File.xml) into a project (I can see it in the Solution Explorer), it resides at the root directory level of the project i.e. the same level as the VB program (.vb), the bin directory and the References directory etc..

I try accessing it using XmlDocument.Load("File.xml") ... but it doesn't find it. I get

A first chance exception of type 'System.IO.FileNotFoundException' occurred in System.Xml.dll

Any idea where the file is or how I 'address' it?

Thanks for any help

Oliver

Upvotes: 1

Views: 6919

Answers (4)

TheGrum
TheGrum

Reputation: 68

An alternative to Include or Content is to set the Build Action to Embedded Resource. Then it will be included in the executable, and you can access it like:

XmlDocument.Load(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(GetType(Me), FileName))

This is the method I use, and it works great. Do note that GetManifestResourceStream returns a Stream, not a String, so you need .Load, not .LoadXML for this method.

Upvotes: 0

Donald.Record
Donald.Record

Reputation: 301

Try this:

XmlDocument.LoadXml(System.IO.Path.GetFullPath(Application.StartupPath & "\File.xml"))

It's worth a shot I guess. My question: is it possible to edit a file that's placed in the project?

Upvotes: 0

Zarkos
Zarkos

Reputation: 457

In the Publish property go to

Application Files --> Look for the XML file: If the publish status = Data File, then the File will be copied to the Application DATA folder.

If you want the xml to be inside the programs directory change the publish status to: Include.

This will do the trick.

Upvotes: 1

carlosfigueira
carlosfigueira

Reputation: 87308

Is the file copied to the output directory? Click on the file, look at the properties in the VS editor, and set the "Build Action" to Content and "Copy to Output Directory" to either "Copy always" or "Copy if newer" (depending on what you need).

Upvotes: 3

Related Questions