简体   繁体   English

在程序集中找不到嵌入式模式

[英]Cannot find the embedded schemas in the assembly

I have DefaultSchemaSet.xsd. 我有DefaultSchemaSet.xsd。 Now I'm getting FileNotFoundException for the codes below. 现在,我得到下面代码的FileNotFoundException。 Give me any suggestion, please? 请给我任何建议吗? May I know how to solve this? 我可以知道如何解决这个问题吗?

public static void GetDefaultSchemas(string path, XmlSchemaSet schemas, ValidationEventHandler schemaValidationEventHandler)
    {
        using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(path))
        {
            if (stream == null)
            {
                throw new FileNotFoundException("Cannot find the embedded schemas in the assembly!");
            }

            var schema = XmlSchema.Read(stream, schemaValidationEventHandler);
            schemas.Add(schema);
        }
    }

In order to make sure you can access it from your code you need to ensure that the file's build action is set to "Embedded Resource" 为了确保您可以从代码中访问它,需要确保将文件的生成操作设置为“嵌入式资源”

To help further we really need to see where the file lies in your solution (to give you an exact answer), however in the mean time if you ensure that your parameter "path" follows the pattern: 为了进一步提供帮助,我们确实需要查看文件在解决方案中的位置(为您提供准确的答案),但是与此同时,如果要确保参数“ path”遵循以下格式:

[DefaultNamespace].[AnySubFolders].[filename.fileextension]

note without the square brackets 注意不带方括号

Check the format of the resource name: 检查资源名称的格式:

DefaultNamespace[.Subfolder][...MoreSubfolers].FileName[.extension]

You need to set Build Action to Embedded Resource in project's file's properties. 您需要在项目文件的属性中将“构建操作”设置为“嵌入式资源”。

嵌入式资源

Also, you need to check the namespace you use for your project: 另外,您需要检查用于项目的名称空间: 在此处输入图片说明

Try to examine the available resources, so you can find if a particular one present: 尝试检查可用资源,以便您查找是否存在特定资源:

        var executingAssembly = Assembly.GetExecutingAssembly();
        var resourceNames = executingAssembly.GetManifestResourceNames();
        foreach (var resourceName in resourceNames)
        {
            Console.WriteLine("Resource: " + resourceName);
            Console.WriteLine("Contents:");
            using (var sr = new StreamReader(executingAssembly.GetManifestResourceStream(resourceName)))
            {
                Console.WriteLine(sr.ReadToEnd());
            }
        }

Output: 输出:

Resource: EmbeddingTests.TextFile1.txt
Contents:
Hello
Resource: EmbeddingTests.NewFolder1.TextFile2.txt
Contents:
Hello 2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM