Results 1 to 11 of 11

Thread: [RESOLVED] Read a file from embedded resources

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2009
    Posts
    116

    Resolved [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.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Sep 2009
    Posts
    116

    Re: Read a file from embedded resources

    Okay, I've added xml_file to resources.

    what's the code for loading it into xmlDoc?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Sep 2009
    Posts
    116

    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.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Sep 2009
    Posts
    116

    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?

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Read a file from embedded resources

    There is no path. The file contents is embedded in your EXE.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Sep 2009
    Posts
    116

    Re: Read a file from embedded resources

    Thank you, that's clear.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Sep 2009
    Posts
    116

    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?

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Read a file from embedded resources

    Quote Originally Posted by Ron Miel View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Sep 2009
    Posts
    116

    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
  •  



Click Here to Expand Forum to Full Width