locked
cannot make xml file an embedded resource RRS feed

  • Question

  • In Visual Studio 2008 I tried to add xml file as embedded resource. During compilation the embedded resource item was simply ignored and the resource I expected to find in resulting dll was not there. I tried several times with no positive effect. Finally I decided to rename the file, so it does not have xml extension. As soon as I changed the extension to (it was .en in this case) Visual Studio started picking the file up and embedding it as part of the assembly.

    I could not find any any documentation on that odd behavior. Does anyone know if that is expected, and, if so, why ?
    What is the correct way of embedding xml file as a resource ?

    Thank you.


    software developer
    Thursday, September 29, 2011 9:29 PM

Answers

  • Hi Marek,

    Sorry, I have a little confused.

    I have show you two worked ways, the one is my post at Friday, September 30, 2011 6:17 AM, and the other is followed your way, it is at Tuesday, October 04, 2011 3:31 AM.

     So which way does not work on your side?

    To be more clear, I repeat the two work ways:

    The frist one:

    1. Go to the property page of your project.

    2. Click on the resource tab.

    3. Click on add resource and choose add exsiting file. (My test file is  log.xml)

    4. Choose your xml file.

    5. Rebuild your application.

    The access code is something like this:  Dim logString As String = My.Resources.log

    The second one:

    1. Right click on the project name.

    2. Add -> Existing Item...

    3. Choose the test file, and then click "Add"  (Here, my test file is XMLFile1.xml)

    4. Set the Build action property to "Embedded resource"

    The access code is something like this: Dim xml As Stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("WinApp_XMLDataSource.XMLFile1.xml")

    Did I have missed something, if so, please feel free to let me know.

    Best regards,


    Mike Feng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Mike Feng Thursday, October 20, 2011 3:31 AM
    Wednesday, October 5, 2011 7:48 AM

All replies

  • Hi Marek,

    Welcome to the MSDN Forum.

    I didn'r reproduce your scenario, but the different is my project type is an windows application, it is not a class libraruy.

    Anyway, I suggest you try this way to add a resource:

    1. Go to the property page of your project.

    2. Click on the resource tab.

    3. Click on add resource and choose add exsiting file.

    4. Choose your xml file.

    5. Rebuild your application.

    I hope this will be helpful.

    Best regards,


    Mike Feng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, September 30, 2011 6:17 AM
  • Mike,
    adding those files the way you described (through project's Resource properties) works. However, before I stepped on the whole problem, I was executing these 3 steps:

    1.add a file to the project using Add -> Existing Item
    2.go to file's Properties
    3.change "Build Action" to "Embedded resource"

    After that the resource we accessible by calling Assembly.GetExecutingAssembly().GetManifestResourceStream(NameSpace.resourceName)

    This has worked for us so far but I we could not get it right with Xml files. I wonder if the way I described is actually correct at all? Any comment would be appreciated.

    Thank you,
    Marek.


    software developer
    Friday, September 30, 2011 2:48 PM
  • Hi Marek,

    Generally, Your way is used to add a form or code file. This is why you need to use reflection to access the resource. So here, I recommand you come back to the normal way. If you have to do this due to some special requirements, plese tell me them, I will try to find a more reasonable way to achieve this goal.

    Best regards,


    Mike Feng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, October 3, 2011 3:27 AM
  • Thank you for your help Mike.  I can get my Xml linked as resource so I am fine.

    The only issue that I'd like to get more details on is why "Embedded resource" way does not work for Xml files. It is very straightforward and for a single file resource it seems to be the best choice. Perhaps there is a white paper or some other piece of documentation you could point at that explain this subject ?

    Thank you,
    Marek.


    software developer
    Monday, October 3, 2011 1:01 PM
  • Hi Marek,

    Please try this code, it can return a xml file string:

            Dim xml As Stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("WinApp_XMLDataSource.XMLFile1.xml")
            Dim binaryData(xml.Length - 1) As Byte
            xml.Read(binaryData, 0, xml.Length)
            'here, which string is correct depends on the xml file encoding
            Dim xmlString As String = Encoding.ASCII.GetString(binaryData)
            'Dim xmlString As String = Encoding.Unicode.GetString(binaryData)
            'Dim xmlString As String = Encoding.UTF8.GetString(binaryData)
            Console.WriteLine(xmlString)
    

    I hope this will be helpful.

    Best regards,


    Mike Feng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, October 4, 2011 3:31 AM
  • Mike,
    as per your advise (and thank you for that) I have attached Xml file as managed resource through Resource.resx With this approach I do not have to go through GetManifestResourceStream() method, let alone UTF8 encoding part. I can get it just by executing a single call Resource.ResourceManager().GetString(myResourceName). That's all I need to get my Xml content.

    My question is different.
    When I link a Xml file as "Embedded resource" (described in my post from Sept 30) the compiler does not actually embed the file at all. So calling GetManifestResourceStream, which is the way you proposed, cannot help becuase resource is not in the assembly.
    As soon as I change the extension of the file to something else (i.e. .sql) the file gets embedded as a resource.

    Do you know why this happens ? Is there any documentation that explains that detail ?

    Thank you,

    Marek

     


    software developer
    Tuesday, October 4, 2011 6:38 PM
  • Hi Marek,

    Sorry, I have a little confused.

    I have show you two worked ways, the one is my post at Friday, September 30, 2011 6:17 AM, and the other is followed your way, it is at Tuesday, October 04, 2011 3:31 AM.

     So which way does not work on your side?

    To be more clear, I repeat the two work ways:

    The frist one:

    1. Go to the property page of your project.

    2. Click on the resource tab.

    3. Click on add resource and choose add exsiting file. (My test file is  log.xml)

    4. Choose your xml file.

    5. Rebuild your application.

    The access code is something like this:  Dim logString As String = My.Resources.log

    The second one:

    1. Right click on the project name.

    2. Add -> Existing Item...

    3. Choose the test file, and then click "Add"  (Here, my test file is XMLFile1.xml)

    4. Set the Build action property to "Embedded resource"

    The access code is something like this: Dim xml As Stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("WinApp_XMLDataSource.XMLFile1.xml")

    Did I have missed something, if so, please feel free to let me know.

    Best regards,


    Mike Feng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Mike Feng Thursday, October 20, 2011 3:31 AM
    Wednesday, October 5, 2011 7:48 AM
  • Mike, the second way (4 steps) is the one which does not work.
    As long as the file extension is ".xml" it is not linked/embedded in the assembly.
    As soon as I change the extension of the file to something else (i.e. ".abc") they get linked.
    I run several tests and the result was always the same.

    I run Visual Studio 2010 SP1 Premium Edition.  At this point I am really looking for explanation what is going on.
    I'd appreciate some explanation.

    Thank you,
    Marek.

     

     


    software developer
    Friday, October 7, 2011 2:57 PM
  • Hi Marek,

    I have test the way on several machines, they all work fine. Can you try this way in a whole new project or anther PC? This may be caused by some broken settings in your orginal project. And here is my test project, you can download it and check for this issue: https://9wmxjg.blu.livefilestore.com/y1pyGaYXgPcyrTa8bX2JKAHCISuQ9rgYxmt40LX6vuzfzuDQ30D5hYG3cAQ1igRzN7ATalEeEzvA813Y1jKJOxI0Q/WinApp-XMLDataSource.rar?download&psid=1

    Best regards,


    Mike Feng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    • Edited by Mike Feng Thursday, October 20, 2011 3:29 AM Add downloading resource
    Monday, October 10, 2011 3:37 AM
  • Hi Marek,

    Any update?

    How about my last suggestion? Is it helpful?

    Did you have encounter any further questions? If so, please feel free to follow up.

    Best regards,


    Mike Feng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, October 17, 2011 3:55 AM