I have a Class Library project which houses some shared code between other projects in my solution. One of these pieces of shared code involves running an XML validation against an XSD file. The name of the XSD is passed as a parameter to the method and then loaded using Assembly.GetFile().
The problem is that the XSD file imports two other XSDs. I’ve loaded all three as Resources within my Class Library but from what I’ve read the xsd:import is not going to work. Is there an alternative approach to making these XSDs available within my Class Library Project without breaking the xsd:import statements?
Edit – Update
I implemented Alexander’s suggestion below but as I stated in my comment, whenever GetEntity() is called for an xs:import‘d XSD, ofObjectToReturn is null. This caused the first instance of an xs:import‘d type to throw an exception “type not defined.”
In an effort to resolve this issue I altered GetEntity() to return GetManifestResourceStream() regardless of ofObjectToReturn‘s value. This now seems to work for the first level of xs:import statements but a secondary xs:import inside one of the original xs:import XSDs is not working. I’ve confirmed that GetEntity() is being called for this secondary xs:import but I’m receiving the “type not defined” exception for a type defined within this secondary XSD.
- TopLevel.xsd – types resolve fine
- FirstLevelImport1.xsd – types resolve fine
- FirstLevelImport2.xsd – types resolve fine
- SecondLevelImport1.xsd – “type not defined” exception thrown for type defined in this XSD
The “type not defined” exception is thrown during XmlReader.Create() that is passed the XmlReaderSettings defining the schema validation.
To resolve the files, which are added by either
xsd:importorxsd:includeyou can use a custom XmlResolver. You can find an example of an ResourceXmlResolver below. It assumes, that the assembly’s name is “AYez.EmbeddedXsdTests“.