Kristoffer
Kristoffer

Reputation: 626

Embedd a xsd file in C#

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.

Upvotes: 4

Views: 5515

Answers (3)

Marc Gravell
Marc Gravell

Reputation: 1063013

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.

Upvotes: 1

Wim
Wim

Reputation: 12082

Use:

Stream xsdStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("schema.xsd");

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038930

// 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));
}

Upvotes: 5

Related Questions