Using the following MSDN documentation I validate an XML file against a schema: http://msdn.microsoft.com/en-us/library/8f0h7att%28v=vs.100%29.aspx
This works fine as long as the XML contains a reference to the schema location or the inline schema. Is it possible to embed the schema "hard-coded" into the application, i.e. the XSD won't reside as a file and thus the XML does not need to reference it?
I'm talking about something like:
- Load XML to be validated (without schema location).
- Load XSD as a resource or whatever.
- Do the validation.
You can use the XmlReaderSettings.Schemas property to specify which schema to use. The schema can be loaded from a Stream.
Yes, this is possible. Read the embedded resource file to
string
and then create yourXmlSchemaSet
object adding the schema to it. Use it in yourXmlReaderSettings
when validating.Try this:
You could declare the XSD as an embedded resource and load it via GetManifestResourceStream as described in this article: How to read embedded resource text file