xsd:import inside an Embedded Resource XSD
I have a Class Library project which houses some shared code between other projects in my solution. One of these pieces of shared code involves running an XML validation against an XSD file. The name of the XSD is passed as a parameter to the method and then loaded using Assembly.GetFile(开发者_如何学Python)
.
The problem is that the XSD file imports two other XSDs. I've loaded all three as Resources within my Class Library but from what I've read the xsd:import is not going to work. Is there an alternative approach to making these XSDs available within my Class Library Project without breaking the xsd:import
statements?
Edit - Update
I implemented Alexander's suggestion below but as I stated in my comment, whenever GetEntity()
is called for an xs:import
'd XSD, ofObjectToReturn
is null
. This caused the first instance of an xs:import
'd type to throw an exception "type not defined."
In an effort to resolve this issue I altered GetEntity()
to return GetManifestResourceStream()
regardless of ofObjectToReturn
's value. This now seems to work for the first level of xs:import
statements but a secondary xs:import
inside one of the original xs:import
XSDs is not working. I've confirmed that GetEntity()
is being called for this secondary xs:import
but I'm receiving the "type not defined" exception for a type defined within this secondary XSD.
- TopLevel.xsd - types resolve fine
- FirstLevelImport1.xsd - types resolve fine
- FirstLevelImport2.xsd - types resolve fine
- SecondLevelImport1.xsd - "type not defined" exception thrown for type defined in this XSD
The "type not defined" exception is thrown during XmlReader.Create()
that is passed the XmlReaderSettings
defining the schema validation.
To resolve the files, which are added by either xsd:import
or xsd:include
you can use a custom XmlResolver. You can find an example of an ResourceXmlResolver below. It assumes, that the assembly's name is "AYez.EmbeddedXsdTests".
using System.Xml;
using System.Xml.Schema;
using NUnit.Framework;
namespace AYez.EmbeddedXsdTests
{
[TestFixture]
public class EmbeddedXsdTests
{
[Test]
public void SomeEntryPoint()
{
var schemaSet = new XmlSchemaSet {XmlResolver = new ResourceXmlResolver()};
schemaSet.Add("rrn:org.xcbl:schemas/xcbl/v4_0/financial/v1_0/financial.xsd", @"Invoice.xsd");
schemaSet.Compile();
var settings = new XmlReaderSettings { ValidationType = ValidationType.Schema, Schemas = schemaSet };
settings.ValidationEventHandler += delegate(object o, ValidationEventArgs e)
{
switch (e.Severity)
{
case XmlSeverityType.Error:
Console.Write("Error: {0}", e.Message);
break;
case XmlSeverityType.Warning:
Console.Write("Warning: {0}", e.Message);
break;
}
};
var xmlReader = XmlReader.Create(@"d:\temp\Invoice.xml", settings);
while (xmlReader.Read()) { /*TODO: Nothing*/} // Validation is performed while reading
}
}
public class ResourceXmlResolver: XmlResolver
{
/// <summary>
/// When overridden in a derived class, maps a URI to an object containing the actual resource.
/// </summary>
/// <returns>
/// A System.IO.Stream object or null if a type other than stream is specified.
/// </returns>
/// <param name="absoluteUri">The URI returned from <see cref="M:System.Xml.XmlResolver.ResolveUri(System.Uri,System.String)"/>. </param><param name="role">The current version does not use this parameter when resolving URIs. This is provided for future extensibility purposes. For example, this can be mapped to the xlink:role and used as an implementation specific argument in other scenarios. </param><param name="ofObjectToReturn">The type of object to return. The current version only returns System.IO.Stream objects. </param><exception cref="T:System.Xml.XmlException"><paramref name="ofObjectToReturn"/> is not a Stream type. </exception><exception cref="T:System.UriFormatException">The specified URI is not an absolute URI. </exception><exception cref="T:System.ArgumentNullException"><paramref name="absoluteUri"/> is null. </exception><exception cref="T:System.Exception">There is a runtime error (for example, an interrupted server connection). </exception>
public override object GetEntity(Uri absoluteUri, string role, Type ofObjectToReturn)
{
// If ofObjectToReturn is null, then any of the following types can be returned for correct processing:
// Stream, TextReader, XmlReader or descendants of XmlSchema
var result = this.GetType().Assembly.GetManifestResourceStream(string.Format("AYez.EmbeddedXsdTests.{0}",
Path.GetFileName(absoluteUri.ToString())));
// set a conditional breakpoint "result==null" here
return result;
}
/// <summary>
/// When overridden in a derived class, sets the credentials used to authenticate Web requests.
/// </summary>
/// <returns>
/// An <see cref="T:System.Net.ICredentials"/> object. If this property is not set, the value defaults to null; that is, the XmlResolver has no user credentials.
/// </returns>
public override ICredentials Credentials
{
set { throw new NotImplementedException(); }
}
}
}
The problem can be fixed if we first read the xsd files into string variable.
e.g.
var stream =assembly.GetManifestResourceStream("Namespace.child.xsd");
now use stream reader to read this into string
e.g.
string childXSD=new StreamReader(stream).ReadToEnd();
similary get string ParentXSD Then use
var xmlReader=XmlReader.Create(new MemoryStream(Encoding.Default.GetBytes(childXSD)));
schemaCollection.Add(null,xmlReader);
var xmlReader=XmlReader.Create(new MemoryStream(Encoding.Default.GetBytes(parentXSD)));
schemaCollection.Add(null,xmlReader);
I think sequence of child to parent is important as we are referencing child XSD into parent.
精彩评论