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
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"
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With