files in resources?

D

Dan

I have a C# DLL file using some small XSD files to validate XML input, some
XML files to store internal parameters, and some XSLT files to transform XML
files. These files are not intended to be modified by users and are required
by the DLL, so I think I should embed them in the DLL assembly possibly as a
resource, and then retrieve and use it internally when required. This way I
won't have to worry about their location (for the DLL to use them) and for
unwanted changes eventually made by users.

I can easily add any XML/XSD/XSLT files to my DLL project as embedded
resources, but my question is: how do I then retrieve them via
ResourceManager, and how can I use them e.g. in functions which require a
stream input e.g. to validate or transform a XML file? Any code snippet or
hint is welcome...

Thanx!
 
B

Bret Mulvey [MS]

Dan said:
I can easily add any XML/XSD/XSLT files to my DLL project as embedded
resources, but my question is: how do I then retrieve them via
ResourceManager, and how can I use them e.g. in functions which require a
stream input e.g. to validate or transform a XML file? > hint is
welcome...

Here's some sample code:

Assembly assembly = Assembly.GetExecutingAssembly();
Stream stream =
assembly.GetManifestResourceStream("DefaultNamespace.XMLFile1.xml");
XmlDocument doc = new XmlDocument();
doc.Load(stream);
stream.Close();
string helloWorld = doc.InnerText;

The only trick is that the name of the resource stream is the name of the
file appended to the name of the project's default namespace. If you have
any question about how your files are getting named, use the
GetManifestResourceNames method to look at the actual names.
 

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