public static void CheckMyFile(string xsd, string xml) { XmlSchemaSet myschema = new XmlSchemaSet(); try { myschema.Add(null, xsd); ValidationShema.ValidateAgainstSchema(xml, myschema); LogXML.SaveXmlLog(Path.GetFileNameWithoutExtension(xml)).Save("Xml_report/" + Path.GetFileName(xml)); Process.Start("explorer.exe", Path.GetDirectoryName(Application.ExecutablePath) + @"\Xml_report"); } catch (Exception e) { CheckImportMOXML.setLog("Неверный файл XSD-схемы!\n" + e.Message); } }
public void HandleValidationError(object sender, ValidationEventArgs ve) { if (ve.Severity == XmlSeverityType.Error || ve.Severity == XmlSeverityType.Warning) { CheckImportMOXML.setLog(string.Join(", ", ValidationShema.stack)); CheckImportMOXML.setLog(ValidationShema.current_el); string[] err_el = { ValidationShema.current_el, ValidationShema.valueZAP, ValidationShema.valueSLUCH, ValidationShema.valueSERV, ve.Exception.Message, ve.Exception.LineNumber.ToString(), ve.Exception.LinePosition.ToString(), ValidationShema.stack.First() }; myValidationErrors.Add(err_el); } }
public static void ValidateAgainstSchema(string XMLSourceDocument, XmlSchemaSet validatingSchemas) { if (validatingSchemas == null) { throw new ArgumentNullException("Серьезная ошибка! Схема не загружена."); } ValidationHandler handler = new ValidationHandler(); XmlReaderSettings settings = new XmlReaderSettings(); settings.CloseInput = true; settings.ValidationType = ValidationType.Schema; settings.ValidationEventHandler += new ValidationEventHandler(handler.HandleValidationError); settings.Schemas.Add(validatingSchemas); settings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings | XmlSchemaValidationFlags.ProcessIdentityConstraints | XmlSchemaValidationFlags.ProcessInlineSchema | XmlSchemaValidationFlags.ProcessSchemaLocation; try { using (XmlReader validatingReader = XmlReader.Create(XMLSourceDocument, settings)) { while (validatingReader.Read()) { if (validatingReader.NodeType == XmlNodeType.Element) { current_el = validatingReader.Name; if (depth == validatingReader.Depth) { stack.Push(validatingReader.Name); depth++; } if (current_el == "N_ZAP") { validatingReader.Read(); valueZAP = validatingReader.Value; } if (current_el == "IDCASE") { validatingReader.Read(); valueSLUCH = validatingReader.Value; } if (current_el == "IDSERV") { validatingReader.Read(); valueSERV = validatingReader.Value; } } else if (validatingReader.NodeType == XmlNodeType.EndElement) { stack.Pop(); depth--; if (current_el == "ZAP") { valueZAP = "[exclude]"; } if (current_el == "Z_SL") { valueSLUCH = "[exclude]"; } if (current_el == "USL") { valueSERV = "[exclude]"; } } } } } catch (Exception ex) { //setLog(ex.Data.ToString()) } if (handler.MyValidationErrors.Count > 0) { foreach (String[] item in handler.MyValidationErrors) { all_err.Add(item); } } else { CheckImportMOXML.setLog("Успешно прошел проверку!"); } }
public MENU() { InitializeComponent(); xsd_check = new CheckImportMOXML(this); }