How to read Embedded Resource in c#
December 14, 2009 6 Comments
Title : How to read Embedded Resource in c#
Publish Date : 14/12/2009
Version : 1.0
Author : Nasser Hajloo
Author Mail : firstname.lastname@example.org
Copyright (c) 2009 Nasser Hajloo.
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation;
with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled “GNU Free Documentation License”.
what is Embedded Resource?
Embedded Resources are whatever developer want to usein his/her assembly without informing end user that he/she is using them. this type of resources will embedd in assembly and will increasing assembly size. the important note about the embedded resource is that this type of files never resultto application change, because their embedd to assembly and end user can’t change them, so if you have and important resource that lack of that file will result to application error you should emmded that resource as an Emmbeded Resource.
Embedded Resource and Resource are so different and Resources will appear to end user and him/her can shange it, usually application translation packs are the commonly exampleof this type of resource, and end user with changing this file willface with translated application.
How to Embedd a Resource? embedding a resource in dot net is as easy of drinking a cup of coffee!. to embedd a resource, all you need is selct the object on Solution Explorer and then press F4 to open Properties window, in properties window you should , from the Advanced section you should select Embedded Reource in Build Action Property. all of this proccess showed in side picture.
using each type of embedded resource is different from others, and how to use an embedded image is different from how to use embedded xml resource. in this article I will describe how to use a xml embedded resource.
How To Read / Write into XML Embedded Resource? because of embeding xml into assembly, you should use a little Reflection to read/write data into embedded xml resource. all you should do is calling xml from inside of the assembly. for example
System.Reflection.Assembly asm = Assembly.GetExecutingAssembly(); System.IO.Stream xmlStream = asm.GetManifestResourceStream("Hajloo.FOSS.UnicodeConverter.EnterpriseAppUnit.ucd.all.flat.xml"); XmlReader reader = XmlReader.Create(xmlStream, settings);
in this example we create an object for our assembly (asm) and then use a Stream (you can use a xmlTextReader or vice versa) to read the xml, in this line of code we address the xml with complete path of XML. note that my XML name is ucd.all.flat.xml and dot between xml do not affect the clr to check another path, you should use complete Path + Name + Extension of the XML document.
in final line of code I create a XMLReader object (you can make a XMRWriter object to write inside of XML) to read (write) data from XML.
trick : if you can not find a correct path to your XML, in windows explorer right click on your assembly and then open it on Notepad++ or other editor. you can find all of you XML document inside of the assembly and you can see the fullpath of XML document here.