Can't load a manifest resource with GetManifestResourceStream()

C#.NetConfigurationsection

C# Problem Overview


I've created a custom configuration section using XSD. In order to parse the config file that follows this new schema, I load the resource (my .xsd file) with this:

public partial class MonitoringConfiguration
    {
        public const string ConfigXsd = "MonitoringAPI.Configuration.MonitoringConfiguration.xsd";
        public const string ConfigSchema = "urn:MonitoringConfiguration-1.0";

        private static XmlSchemaSet xmlSchemaSet;

        static MonitoringConfiguration()
        {
            xmlSchemaSet = new XmlSchemaSet();
            Stream xsdStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(ConfigXsd);
            XmlReader schemaReader = XmlReader.Create(xsdStream);
            xmlSchemaSet.Add(ConfigSchema, schemaReader);
        }

    }

By the way my resource is: MonitoringConfiguration.xsd. And the namespace of the other partial class (that represents the code behind of the .xsd file) is MonitoringAPI.Configuration.

The problem is situated here:

 Stream xsdStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(ConfigXsd);

The xsdStream is null, so I guess the resource can't be found! But why?

Thank you

C# Solutions


Solution 1 - C#

The name of the resource is always:

<Base namespace>.<RelativePathInProject>.<FileName>

So if your resource is located in "Resources/Xsd/", and your default project namespace is "MonitoringAPI.Configuration", the resource name is:

"MonitoringAPI.Configuration.Resources.Xsd.MonitoringConfiguration.xsd"

Also make sure the build action for your resource is set to "Embedded Resource"

Solution 2 - C#

Easy and correct way to get the actual name of your embedded resource:

string[] resourceNames =
    Assembly.GetExecutingAssembly().GetManifestResourceNames();

Then simply check resourceNames array, and you will know for sure what to pass to GetManifestResourceStream method.

Solution 3 - C#

In my case,

When you try to access the file via GetManifestResourceStream(). You will get an error due to invalid path of the file, and stream will be null.

Solution:

Right click on the file which you have added in to solution and Click on Properties.

Select the Build Action as Embedded Resource. (Instead of Content - by default)

Build action property set to embedded resource

Solution 4 - C#

By default, visual studio does not embed xsd file therefore you must ensure "Build Action" property of xsd file is set to "Embedded Resource" to make it works

Solution 5 - C#

just add your resources under form1.resx -->add existing items

double click on the resources you added under Resources folder.go to properties and select "Embedded Resources" instead of none.

Then try debugging the line:

string[] resourceNames=Assembly.GetExecutingAssembly().GetManifestResourceNames();

check the resources you added are in the array. then copy the resource name exactly from this array and try putting the name on your code..it works fine!!

Solution 6 - C#

> You can get the Resource Stream by passing the Resource Names which is as follows below...

  1. Get the resource name e.g..

Assembly objAssembly = Assembly.GetExecutingAssembly();

string[] strResourceNames = objAssembly.GetManifestResourceNames();

  1. Pass the Resource Names to ...

    Stream strm = objAssembly.GetManifestResourceStream(strResourceNames);

> Now you have Stream you can do whatever you want...

Solution 7 - C#

In my case, it was something completely different:

My UWP App compiled correctly in Debug and Release configuration but GetManifestResourceStream returned Null only Release configuration.

The issue was, that in the UWP Build Configuration file (and only there) the setting "Compile with .NET Native tool chain" was enabled. After disabling, GetManifestResourceStream worked as expected.

Solution 8 - C#

I had an issue where I was embedding a whole heap of .xsd files in many different assemblies; everything was working (GetManifestResourceNames was returning the files I'd expect to see) except for one. The one which wasn't was called:

Something.LA.xsd

I wasn't dealing with specific cultures and the .LA bit at the end of the filename was being picked up by the compiler as this file being for the LA culture - the filename in the manifest was going in as Something.xsd (under culture LA) - hence me not being able to find it (it ended up in a satellite assembly). I dodged the issue by renaming the file - presumably it is possible to explicitly state the culture of a given embedded resource.

Actually, a quick google reveals: https://stackoverflow.com/questions/39477616/how-can-i-prevent-embedded-resource-file-culture-being-set-based-on-its-filename

According to this answer, you've got to do hacky things - so perhaps renaming the file wasn't so bad after all :)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAmokrane ChentirView Question on Stackoverflow
Solution 1 - C#Philippe LeybaertView Answer on Stackoverflow
Solution 2 - C#user1958681View Answer on Stackoverflow
Solution 3 - C#RajeshKdevView Answer on Stackoverflow
Solution 4 - C#Huy ThaiView Answer on Stackoverflow
Solution 5 - C#RittikaView Answer on Stackoverflow
Solution 6 - C#Sanjay DwivediView Answer on Stackoverflow
Solution 7 - C#MarcusView Answer on Stackoverflow
Solution 8 - C#GHCView Answer on Stackoverflow