Use an XmlReader
with an XmlReaderSettings
which specifies the type of validation to perform and a ValidationEventHandler
. This can be wrapped into a method that will give you the schema or schemas against which the XML document was successfully validated.
If you're concerned about new schemas being added in the future, then just store them in a central location like a directory and grab them at runtime. New schemas could simply be dropped into the directory as needed.
void Main()
{
var rootDirectory = @"C:\Testing";
var schemaDirectory = Path.Combine(rootDirectory, "Schemas");
var dataDirectory = Path.Combine(rootDirectory, "Data");
var schemaFiles = new[] {
Path.Combine(schemaDirectory, "IQ.xsd"),
Path.Combine(schemaDirectory, "IMR.xsd"),
Path.Combine(schemaDirectory, "RP.xsd")
};
var dataFiles = new[] {
Path.Combine(dataDirectory, "IQ.xml"),
Path.Combine(dataDirectory, "IMR.xml"),
Path.Combine(dataDirectory, "RP.xml")
};
var results = FindMatchingSchemas(dataFiles[1], schemaFiles).Dump();
Console.WriteLine("Matching schema is: {0}", results.First(r => r.Value));
}
private static Dictionary<string, bool> FindMatchingSchemas(string dataFile, string[] schemaFiles)
{
var results = new Dictionary<string, bool>();
foreach (var schemaFile in schemaFiles)
{
results.Add(schemaFile, true);
// Set the validation settings.
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessInlineSchema;
settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessSchemaLocation;
settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
settings.ValidationEventHandler += new ValidationEventHandler((object sender, ValidationEventArgs args) =>
{
// Validation error
results[schemaFile] = false;
});
settings.Schemas.Add(null, schemaFile);
// Create the XmlReader object.
XmlReader reader = XmlReader.Create(dataFile, settings);
// Parse the file.
while (reader.Read());
}
return results;
}
// Output: Matching schema is: C:\Testing\Schemas\IMR.xsd
There is a free website which can generate XSD documents from XML documents. http://www.freeformatter.com/xsd-generator.html
IQ.xsd
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Catalog">
<xs:complexType>
<xs:sequence>
<xs:element name="Rec">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="ITEM"/>
<xs:element type="xs:short" name="QTY"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
IMR.xsd
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Catalog">
<xs:complexType>
<xs:sequence>
<xs:element name="Rec">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:short" name="ITEMS"/>
<xs:element type="xs:string" name="MFG"/>
<xs:element type="xs:string" name="ROUTE"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
RP.xsd
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Catalog">
<xs:complexType>
<xs:sequence>
<xs:element name="Rec">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="REF"/>
<xs:element type="xs:short" name="PON"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Derived from Validating an XML against referenced XSD in C#