[RESOLVED] Read a file from embedded resources
I'm having trouble with reading a file from embedded resources.
I'm starting to learn about developing applications for eBay. You can pass queries to eBay in XML format, and receive replies back in the same format.
I have some examples of code to do this. Here's how the sample code loads an xml file, containing the query.
Code:
'Get XML Document from Embedded Resources
Dim xmlDoc As New XmlDocument()
Dim current_assembly As Assembly
Dim xml_stream As Stream
current_assembly = Assembly.GetExecutingAssembly()
xml_stream = current_assembly.GetManifestResourceStream("Sample.xml_file.xml")
xmlDoc.Load(xml_stream)
This works in the sample code. But when I try to write my own code, using those exact lines, it doesn't work. The line xml_stream = curr... fails. xml_stream is nothing. And that means that xmlDoc. Load fails.
I can't see any difference between the sample and my attempt.
The Assembly Name and Root Namespace in my project are both set to "Sample," just as in the original.
I've tried things like changing the filename and the namespace, but I still can't get it to work.
Re: Read a file from embedded resources
There's an easier way to use resources. Add the file to your resources on the Resources page of the project properties. You can then get the data from My.Resources in one line of code.
Re: Read a file from embedded resources
Okay, I've added xml_file to resources.
what's the code for loading it into xmlDoc?
Re: Read a file from embedded resources
When you add a file to your project resources, a corresponding property is added to My.Resources. The type of that property depends on the type of file you embed. I've never actually embedded an XML file before so I'm not sure what type it produces. I would guess that it would most likely be a String. If so then you'd create an XmlDocument and call its LoadXml method.
Re: Read a file from embedded resources
I can't work out how to do this.
edit - oh wait, LoadXml method. I didn't see that. I'll try again.
Re: Read a file from embedded resources
Thank you, that worked.
One thing, though. When I use a resource file like this, does it use a relative file path or absolute? If I move the project folder to a different location, will it still work? And if I create an installer for the project, and intall on a different computer, will the XML files be part of the install?
Re: Read a file from embedded resources
There is no path. The file contents is embedded in your EXE.
Re: Read a file from embedded resources
Re: Read a file from embedded resources
One further question, is it possible to alter an embedded resource, e.g. add nodes to an XML file, or change the values of the data?
Re: Read a file from embedded resources
Quote:
Originally Posted by
Ron Miel
One further question, is it possible to alter an embedded resource, e.g. add nodes to an XML file, or change the values of the data?
A resource is binary data stored within your EXE or DLL file. It's not a separate file any more so no, you cannot edit, which is one of the main points of resources. If you want to be able to edit a file then you need a file, so don't embed it as a resource in the first place.
Re: Read a file from embedded resources
Thank you. Much gratitude.