tags:

views:

232

answers:

3

Hi

Right now we have static code to a XML schema file. But now we want to embedd that file

Code right now:

XmlTextReader reader = new XmlTextReader("schema.xsd");
XmlSchema schema = XMLSchema.Read(xReader, new ValidationEventHandler(ValidationEventHandler));

But now I want to have it embedded in a Resouce file. So how do I do.

XmlTextReader reader = new XmlTextReader(Resouces.Schema);
XmlSchema schema = XMLSchema.Read(xReader, new ValidationEventHandler(ValidationEventHandler));

This is not the way.

+2  A: 
// Get the assembly that contains the embedded schema
var assembly = Assembly.GetExecutingAssembly();
using (var stream = assembly.GetManifestResourceStream("namespace.schema.xsd"))
using (var reader = XmlReader.Create(stream))
{
    XmlSchema schema = XMLSchema.Read(
        reader, 
        new ValidationEventHandler(ValidationEventHandler));
}
Darin Dimitrov
A: 

Use:

Stream xsdStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("schema.xsd");
Wim Hollebrandse
A: 

If you have a single file, just extract it (GetManifestResourceStream) and use directly. If you have multiple related files, you'll need to write an XmlResolver. I have a resx-based resolver somewhere... You then set this as the XmlResolver or an XmlReaderSettings, and pass in your XmlReaderSettings when calling XmlReader.Create.

Marc Gravell