Resource file question!

C

CSharper

I am using VS2008 C#3.0 and I would like to know if some one can help
me wiith the following 2 questions;
1. I have a XML file called test.xml file in resource and I would like
to how can I read the xml file wihtout extracting it to a external
file? I would like to load the object into memory and use it from
there.
2. I am planning to have too many data files in the resource file, is
it possible to compress the resource file and then use it?

Thanks.
 
C

CSharper

I don't recall the specifics off the top of my head, but it should be  
possible to get a MemoryStream from a binary resource.  So if you just  
treat the XML file resource as a binary resource, you should be able to  
wrap that in a MemoryStream and then just read it normally as a regular  
stream.


Yes, though I'm not aware of any automatic support for this in the PE  
format (the file format for executables that defines how resources are  
stored).  You can gzip the XML and save the compressed file as your  
resource.  Then use the GzipStream class to decompress it when you are  
reading the file (basically you'll point the GzipStream to the  
MemoryStream, and then read from the GzipStream instead of the  
MemoryStream...you're still reading from a stream though).

Pete

Thank you very much for the help.
 
J

Jeroen Mostert

Peter said:
I don't recall the specifics off the top of my head, but it should be
possible to get a MemoryStream from a binary resource. So if you just
treat the XML file resource as a binary resource, you should be able
to wrap that in a MemoryStream and then just read it normally as a
regular stream.

FYI...I took a quick look and sure enough the binary resource shows up
as a regular byte[]. So you can just use the MemoryStream constructor
that takes a byte[] array as the source.

There's Assembly.GetManifestResourceStream() and
ResourceManager.GetStream(), both of which directly return a stream (saving
the overhead of constructing a byte[] first).

Assembly.GetManifestResourceStream() is most appropriate for resources that
don't need to be localized.
 
J

Jeroen Mostert

Peter said:
There's Assembly.GetManifestResourceStream() and
ResourceManager.GetStream(), both of which directly return a stream
(saving the overhead of constructing a byte[] first).

Which, I suppose, might be important if it's a really big file or
something (tens or hundreds of megabytes, for example).

Otherwise, it seems to me that the simpler code, essentially an
easy-to-read one-liner, is a better choice, even if it's not as
optimal.

I wasn't looking for a fight along the "my idea is better than yours"
divide. Or a discussion on things that I hope everyone can decide for their own.
But you're right...if someone really wants to get a stream directly,
they can.
Let's promote this to my main point and forget about my ill-advised dropping
of the "o" word, lest I be branded as an optimization bum and an enemy of
simplicity for the rest of my life, which would be a shame.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top