我试图使用下面的代码验证架构
string[] assemblyNameParts = schemaStrongName.Split(new char[] { ',' }, PARTS_IN_SCHEMA_STRONG_NAME);
string className = assemblyNameParts[PART_CLASS_NAME].Trim();
string fullyQualifiedAssemblyName = assemblyNameParts[PART_QUALIFIED_ASSEMBLY_NAME].Trim();
Assembly schemaAssembly = Assembly.Load(fullyQualifiedAssemblyName);
Type schemaType = schemaAssembly.GetType(className);
SchemaBase btsSchemaCollection = (SchemaBase)Activator.CreateInstance(schemaType);
XmlReaderSettings xmlReadeSettings = new XmlReaderSettings();
//xmlReadeSettings.DtdProcessing = DtdProcessing.Ignore;
xmlReadeSettings.ValidationType = ValidationType.Schema;
xmlReadeSettings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings | XmlSchemaValidationFlags.ProcessIdentityConstraints;// | XmlSchemaValidationFlags.AllowXmlAttributes;
foreach (XmlSchema xschema in btsSchemaCollection.SchemaCollection)
{
String schemaXMl = xschema.ToString();
xmlReadeSettings.Schemas.Add(xschema);
//XmlSchema schema = XmlSchema.Read(new StringReader(schemaXMl));//, (sender, args) => { });
}
xmlReadeSettings.ValidationEventHandler += new ValidationEventHandler(ValidationHandler);
XmlTextReader xmlTextReader = new XmlTextReader(new StringReader(requestMessage.OuterXml));
XmlReader xmlReader = XmlReader.Create(xmlTextReader, xmlReadeSettings);
while (xmlReader.Read()) ;
xmlReader.Close();
但我发现XMLSchema不是线程安全的,如下面的链接
所述Multithreaded xml validation against schema in .net
现在我的问题是如何将模式缓存为btsSchemaCollection.SchemaCollection的字符串
我需要使用下面的代码
XmlSchema schema = XmlSchema.Read(new StringReader(schemaXml), (sender, args) => { });
settings.Schemas.Add(schema);
并替换下面的代码
foreach (XmlSchema xschema in btsSchemaCollection.SchemaCollection)
{
String schemaXMl = xschema.ToString();
xmlReadeSettings.Schemas.Add(xschema);
//XmlSchema schema = XmlSchema.Read(new StringReader(schemaXMl));//, (sender, args) => { });
}