Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to embed an xml file to a resource file

Tags:

c#-2.0

i want to embed a xml file to a resource file in my project,whenever i need the file i must get it from resource and use it,how to do this and i want to modify the contents of the xml file depending upon my requirements.how to do this

like image 547
karthik Avatar asked Aug 23 '10 13:08

karthik


People also ask

How do you add an embedded resource?

Open Solution Explorer add files you want to embed. Right click on the files then click on Properties . In Properties window and change Build Action to Embedded Resource . After that you should write the embedded resources to file in order to be able to run it.

What is embedded XML?

Embedded expressions enable you to create XML literals that contain expressions that are evaluated at run time. The syntax for an embedded expression is <%= expression %> , which is the same as the syntax used in ASP.NET.


1 Answers

If you add the XML file to a Visual Studio project and, in the Property window for it, select Build Action: Embedded resource, the file will be embedded into the build output artifact for that project.

To access it from code, use something like:

string resourceName = "Namespace.Prefix.FileName.xml";
Assembly someAssembly = LoadYourAssemblyContainingTheResource();
XmlDocument xml = new XmlDocument();
using (Stream resourceStream = someAssembly.GetManifestResourceStream(resourceName))
{
    xml.Load(resourceStream);
}
// The embedded XML resource is now available in: xml

If the resource you're loading is embedded in your own assembly, you can do something like Assembly.GetExecutingAssembly() to achieve what I listed as LoadYourAssemblyContainingTheResource() above, or possibly typeof(SomeTypeInYourResourceAssembly).Assembly

It's unclear what you mean by "want to modify the contents" - you cannot modify the resource inside the assembly at run-time, but whenever you change the XML file and recompile, the new version will be embedded.

like image 127
Cumbayah Avatar answered Oct 20 '22 06:10

Cumbayah