static void LoadOperationalTemplateSchemas(System.Xml.Schema.XmlSchemaSet xs) { if (!xs.Contains(RmXmlSerializer.OpenEhrNamespace)) { System.Xml.Schema.XmlSchema baseTypesSchema = RmXmlSerializer.GetOpenEhrSchema("BaseTypes"); System.Xml.Schema.XmlSchema resourceSchema = RmXmlSerializer.GetOpenEhrSchema("Resource"); resourceSchema.Includes.Clear(); System.Xml.Schema.XmlSchemaInclude include = new System.Xml.Schema.XmlSchemaInclude(); include.Schema = baseTypesSchema; resourceSchema.Includes.Add(include); System.Xml.Schema.XmlSchema archetypeSchema = RmXmlSerializer.GetOpenEhrSchema("Archetype"); archetypeSchema.Includes.Clear(); include = new System.Xml.Schema.XmlSchemaInclude(); include.Schema = resourceSchema; archetypeSchema.Includes.Add(include); System.Xml.Schema.XmlSchema openEhrProfileSchema = RmXmlSerializer.GetOpenEhrSchema("OpenehrProfile"); openEhrProfileSchema.Includes.Clear(); include = new System.Xml.Schema.XmlSchemaInclude(); include.Schema = archetypeSchema; openEhrProfileSchema.Includes.Add(include); System.Xml.Schema.XmlSchema templateSchema = RmXmlSerializer.GetOpenEhrSchema("Template"); templateSchema.Includes.Clear(); include = new System.Xml.Schema.XmlSchemaInclude(); include.Schema = openEhrProfileSchema; templateSchema.Includes.Add(include); xs.Add(templateSchema); xs.Compile(); } }
public static int ValidateXML(AasValidationRecordList recs, Stream xmlContent) { // see: AasxCsharpLibrary.Tests/TestLoadSave.cs var newRecs = new AasValidationRecordList(); // access if (recs == null || xmlContent == null) { return(-1); } // Load the schema files var files = GetSchemaResources(SerializationFormat.XML); if (files == null) { return(-1); } var xmlSchemaSet = new System.Xml.Schema.XmlSchemaSet(); xmlSchemaSet.XmlResolver = new System.Xml.XmlUrlResolver(); try { Assembly myAssembly = Assembly.GetExecutingAssembly(); foreach (var schemaFn in files) { using (Stream schemaStream = myAssembly.GetManifestResourceStream(schemaFn)) { using (XmlReader schemaReader = XmlReader.Create(schemaStream)) { xmlSchemaSet.Add(null, schemaReader); } } } } catch (Exception ex) { throw new FileNotFoundException("ValidateXML: Error accessing embedded resource schema files: " + ex.Message); } // set up messages xmlSchemaSet.ValidationEventHandler += (object sender, System.Xml.Schema.ValidationEventArgs e) => { newRecs.Add(new AasValidationRecord(AasValidationSeverity.Serialization, null, "" + e?.Exception?.LineNumber + ", " + e?.Exception?.LinePosition + ": " + e?.Message)); }; // compile try { xmlSchemaSet.Compile(); } catch (Exception ex) { throw new InvalidOperationException("ValidateXML: Error compiling schema files: " + ex.Message); } if (newRecs.Count > 0) { var parts = new List <string> { $"Failed to compile the schema files:" }; parts.AddRange(newRecs.Select <AasValidationRecord, string>((r) => r.Message)); throw new InvalidOperationException(string.Join(Environment.NewLine, parts)); } // load/ validate on same records var settings = new System.Xml.XmlReaderSettings(); settings.ValidationType = System.Xml.ValidationType.Schema; settings.Schemas = xmlSchemaSet; settings.ValidationEventHandler += (object sender, System.Xml.Schema.ValidationEventArgs e) => { newRecs.Add(new AasValidationRecord(AasValidationSeverity.Serialization, null, "XML: " + e?.Exception?.LineNumber + ", " + e?.Exception?.LinePosition + ": " + e?.Message)); }; // use the xml stream using (var reader = System.Xml.XmlReader.Create(xmlContent, settings)) { while (reader.Read()) { // Invoke callbacks } ; } // result recs.AddRange(newRecs); return(newRecs.Count); }
public void TestLoadSaveXmlValidate() { // Load the schema var xmlSchemaSet = new Xml.Schema.XmlSchemaSet(); xmlSchemaSet.XmlResolver = new Xml.XmlUrlResolver(); string schemaPath = Path.Combine( TestContext.CurrentContext.TestDirectory, "Resources\\schemas\\xml\\AAS.xsd"); xmlSchemaSet.Add(null, schemaPath); var schemaMessages = new List <string>(); xmlSchemaSet.ValidationEventHandler += (object sender, Xml.Schema.ValidationEventArgs e) => { schemaMessages.Add(e.Message); }; xmlSchemaSet.Compile(); if (schemaMessages.Count > 0) { var parts = new List <string> { $"Failed to compile the schema: {schemaPath}" }; parts.AddRange(schemaMessages); throw new InvalidOperationException(string.Join(Environment.NewLine, parts)); } // Load-Save-Validate List <string> aasxPaths = SamplesAasxDir.ListAasxPaths(); using (var tmpDir = new TemporaryDirectory()) { string tmpDirPath = tmpDir.Path; foreach (string aasxPath in aasxPaths) { using (var package = new AdminShellPackageEnv(aasxPath)) { string name = Path.GetFileName(aasxPath); string outPath = System.IO.Path.Combine(tmpDirPath, $"{name}.converted.xml"); package.SaveAs(outPath, writeFreshly: true); var settings = new Xml.XmlReaderSettings(); settings.ValidationType = Xml.ValidationType.Schema; settings.Schemas = xmlSchemaSet; var messages = new List <string>(); settings.ValidationEventHandler += (object sender, Xml.Schema.ValidationEventArgs e) => { messages.Add(e.Message); }; using (var reader = Xml.XmlReader.Create(outPath, settings)) { while (reader.Read()) { // Invoke callbacks } ; if (messages.Count > 0) { var parts = new List <string> { $"Failed to validate XML file exported from {aasxPath} to {outPath}:" }; parts.AddRange(messages); throw new AssertionException(string.Join(Environment.NewLine, parts)); } } } } } }