Frequently, when writing code, you are faced with a situation where your code depends on these twiddle files, icons, css, javascript, xsd schemas etc, and don’t we all LOVE to be able to ship a single dll for easy maintenance?
.NET makes it darned easy to do so.
I just posted a tip about validating input XML files with an XML Schema. The issue is, I don’t want the user to have to bother with XSD files when using my code. So I embedded them inside the DLL. Here is how. (Note, you can use the below instructions for anything – not just XSD’s).
Step 1: Add the XSD to your class library project just as you would normally
Step 2: Right click à properties on the XSD file, and under Build Action, choose “Embedded Resource”
Step 3: Modify the code as shown here from
XmlSchemaSet schemaSet = new XmlSchemaSet() ;
schemaSet.Add("", XmlReader.Create(xmlSchema));
to
TextReader schemaStream =
new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream(xmlSchema));
XmlSchemaSet schemaSet = new XmlSchemaSet() ;
schemaSet.Add("",XmlReader.Create(schemaStream));
That’s it !! J
On
2/7/2007 6:21:30 PM
Paul
said ..
This solution requires the XML file to specify the targetNamespace of the schema file.
How can this limitation be removed? The XML file should (preferably) not need to know about the schema...
|