Search code examples
c#xsd.net-assembly

Cannot find the embedded schemas in the assembly


I have DefaultSchemaSet.xsd. Now I'm getting FileNotFoundException for the codes below. 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);
        }
    }

Solution

  • 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.

    Embedded resource

    Also, you need to check the namespace you use for your project: enter image description here

    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