无法加载与GetManifestResourceStream清单资源()清单、加载、资源、GetManifestResourceStream

2023-09-02 10:30:58 作者:朕很萌′

我已经使用XSD创建一个自定义的配置部分。为了解析遵循这个新的模式配置文件,我加载资源(我.xsd文件)这一点:

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);
        }

    }

顺便说一句我的资源:MonitoringConfiguration.xsd。而其他部分的类的命名空间(即重新presents的.xsd文件背后的code)的 MonitoringAPI.Configuration

问题是坐落于此:

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

该xsdStream为空,所以我想,资源不能被发现!但是,为什么?

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

感谢您

推荐答案

该资源的名称始终是:

< Base命名空间>< RelativePathInProject><文件名>

因此​​,如果您的资源位于资源/ XSD /,而默认的项目命名空间是MonitoringAPI.Configuration,该资源名称是:

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"