无法使用 GetManifestResourceStream() 加载清单资源
我使用 XSD 创建了一个自定义配置部分。为了解析遵循这个新架构的配置文件,我使用以下内容加载资源(我的 .xsd 文件):
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。另一个分部类(代表 .xsd 文件后面的代码)的命名空间是 MonitoringAPI.Configuration
。
问题出在这里:
Stream xsdStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(ConfigXsd);
xsdStream 为空,所以我猜找不到资源!但为什么?
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
资源的名称始终为:
因此,如果您的资源位于“Resources/Xsd/”中,并且您的默认项目命名空间为“MonitoringAPI.Configuration”,资源名称为:
“MonitoringAPI.Configuration.Resources.Xsd.MonitoringConfiguration.xsd”
另外,请确保资源的构建操作设置为“嵌入式资源”
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"
获取嵌入资源的实际名称的简单而正确的方法:
然后只需检查 resourceNames 数组,您就会知道要传递给 GetManifestResourceStream 方法的内容。
Easy and correct way to get the actual name of your embedded resource:
Then simply check resourceNames array, and you will know for sure what to pass to GetManifestResourceStream method.
就我而言,
当您尝试通过 GetManifestResourceStream() 访问文件时。由于文件路径无效,您将收到错误,并且流将为空。
解决方案:
右键单击已添加到解决方案中的文件,然后单击“属性”。
选择
构建操作
作为嵌入式资源
。 (而不是Content
- 默认情况下)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
asEmbedded Resource
. (Instead ofContent
- by default)默认情况下,Visual Studio 不嵌入 xsd 文件,因此您必须确保 xsd 文件的“构建操作”属性设置为“嵌入资源”才能使其正常工作
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
只需在 form1.resx 下添加您的资源 --> 添加现有项目,
双击您在资源文件夹下添加的资源。转到属性并选择“嵌入式资源”而不是“无”。
然后
尝试调试该行:
检查您添加的资源是否在数组中。然后从该数组中准确复制资源名称并尝试将名称放入您的代码中..它工作正常!
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:
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!!
获取资源名称,例如。
装配 objAssembly = Assembly.GetExecutingAssembly();
string[] strResourceNames = objAssembly.GetManifestResourceNames();
将资源名称传递给...
Stream strm = objAssembly.GetManifestResourceStream(strResourceNames);
Get the resource name e.g..
Assembly objAssembly = Assembly.GetExecutingAssembly();
string[] strResourceNames = objAssembly.GetManifestResourceNames();
Pass the Resource Names to ...
Stream strm = objAssembly.GetManifestResourceStream(strResourceNames);
就我而言,情况完全不同:
我的 UWP 应用程序在调试和发布配置中正确编译,但 GetManifestResourceStream 仅返回 Null 发布配置。
问题是,在 UWP 构建配置文件中(并且仅在该文件中)启用了设置“使用 .NET Native 工具链编译”。禁用后,GetManifestResourceStream 按预期工作。
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.
我遇到了一个问题,我将一整堆 .xsd 文件嵌入到许多不同的程序集中;一切正常(GetManifestResourceNames 正在返回我期望看到的文件),除了一个。不是的那个被称为:
我没有处理特定的文化,并且文件名末尾的 .LA 位被编译器拾取,因为该文件适用于 LA 文化 - 清单中的文件名是作为 Something.xsd (在文化 LA 下) - 因此我无法找到它(它最终出现在卫星程序集中)。我通过重命名文件来避免这个问题 - 大概可以明确地说明给定嵌入式资源的文化。
事实上,快速谷歌一下就会发现:
如何防止根据文件名设置嵌入式资源文件区域性
根据这个答案,你必须做一些 hacky 的事情 - 所以也许重命名文件并不是那么糟糕:)
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:
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:
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 :)