|
-
Mar 13th, 2010, 07:21 AM
#1
Thread Starter
Lively Member
[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.
-
Mar 13th, 2010, 07:28 AM
#2
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.
-
Mar 13th, 2010, 07:40 AM
#3
Thread Starter
Lively Member
Re: Read a file from embedded resources
Okay, I've added xml_file to resources.
what's the code for loading it into xmlDoc?
-
Mar 13th, 2010, 07:46 AM
#4
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.
-
Mar 13th, 2010, 10:37 AM
#5
Thread Starter
Lively Member
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.
Last edited by Ron Miel; Mar 13th, 2010 at 10:44 AM.
-
Mar 13th, 2010, 11:35 AM
#6
Thread Starter
Lively Member
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?
-
Mar 13th, 2010, 09:40 PM
#7
Re: Read a file from embedded resources
There is no path. The file contents is embedded in your EXE.
-
Mar 14th, 2010, 06:44 PM
#8
Thread Starter
Lively Member
Re: Read a file from embedded resources
-
Mar 16th, 2010, 02:05 PM
#9
Thread Starter
Lively Member
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?
-
Mar 16th, 2010, 06:49 PM
#10
Re: Read a file from embedded resources
 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.
-
Mar 16th, 2010, 08:44 PM
#11
Thread Starter
Lively Member
Re: Read a file from embedded resources
Thank you. Much gratitude.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|