I am trying to validate an XML file against an XSD that is stored in the
assembly as an embedded resource. I can get it to work as long as the XSD
does not include other XSDs. After a fair amount of searching, I have found
3 possible solutions but none have worked for me. They are:
1. Use the Includes property of XmlSchema to add included XSDs, then call
Compile
XmlSchema mainSchema = XmlSchema.Read( stream1, null );
XmlSchema includeSchema1 = XmlSchema.Read( stream2, null );
XmlSchema includeSchema2 = XmlSchema.Read( stream3, null );
mainSchema.Incl udes.Add( includeSchema1 );
mainSchema.Incl udes.Add( includeSchema1 );
mainSchema.Comp ile( null );
I get a runtime exception from the Compile method: "Specified cast is not
valid"
Reference:
http://groups.google.com/groups?q=+....phx.gbl&rnum=1
2. Write a custom XmlResolver class
I derived directly from XmlResolver. In the GetEntity method, I read in
the resource stream based on the uri and return the stream. The type param
is always undefined so I am not sure what I should be returning, as the
return type is object. Should I return the stream, an XML fragement, a
string, etc.?
When I return a stream, it appears to be ignored by the main XSD file. I
get a runtime exception because elements are not declared (those that are
defined in the XSD includes).
Reference:
http://groups.google.com/groups?q=.n...ft.com&rnum=14
3. Use new XmlSchemaSet class in .Net 2.0
We are using v 1.1, so this method is not available to us.
Reference:
http://groups.google.com/groups?q=+....phx.gbl&rnum=1
I know that my XSD files (resources) are correct, because if I specify the
name of the XSD file as the URL param in XmlValidatingRe ader.Schemas.Ad d, it
works fine.
Does anyone know the correct way to load XML Schemas from an embedded
resource with includes?
Thanks,
-Dave Muoio