Search code examples
c#xmlvisual-studio-2012embedded-resource

I can't read my embedded XML file from my second C# project


I have a Visual Studio Solution with two projects inside.

The first project is a windows service, and this project contains one XML file inside a folder (called Configurations). This XML file is called Databases.xml. I have changed Databases.xml Build Action from content to embedded resource, and now I want to access this XML file from my other project in my solution, which is a WPF application.

I have therefore added an reference to my windows service project inside my WPF project, and would now like to access my XML file from my WPF project.

My problem is that when I am trying to access the embedded resource then I can't find out which type to use and what the path/namespace to my assembly and XML file should be. When I am using the

string[] names = this.GetType().Assembly.GetManifestResourceNames();

my names array is filled out with some resources from my WPF project. What I want is to access the ResourceNames and of course my Databases.xml file from my Windows Service project.

Please help me since this problem is driving me nuts.

If you need any additional information, please let me know.

My Solution Update 26-07-2013

I found out that the real problem occured when I couldn't use my first windows Service projects namespace as a type for my assembly. my Windows Service consists of a service class (with OnStart() and OnStop() method inside), and in order to use this class as my namespace type, I needed to add another reference to my WPF project. I needed to add a reference to System.ServiceProcess namespace, in order to use my Windows Service Class as a type for my assembly in my WPF Project.

In Order to access my Databases.xml file, I have come up with this solution. Remember to insert your own projects name and class name instead of my placeholders (<Windows Service Project Name> etc).

//Remember to add a reference to System.ServiceProcess in order to be able to use your WIndows Service Project as an assembly type.
using (Stream stream = typeof(<Windows Service Project Name>.<Windows Service Class Name>).Assembly.GetManifestResourceStream("<Windows Service Project Name>.<Folder Name>.Databases.xml"))
{
    //Load XML File here, for instance with XmlDocument Class
    XmlDocument doc = new XmlDocument();
    doc.Load(stream);
}

So my real problem was that I didn't include the System.ServiceProcess reference to my second project.


Solution

  • You've to refer to Windows Service project Assembly to make it work

    The problem with your code is This.GetType().Assesmbly gives current Assembly In your case WPF Assembly and obviously you'll not find what you need there.

    Try this

    Assembly windowsServiceAssembly = typeof(SomeTypeFromThatAssembly).Assembly;
    string[] names = windowsServiceAssembly.GetManifestResourceNames();
    

    Hope this helps.