public void Validate(Application excelApp) { var xmlFileName = dialogService.AskForFiles(false, FileType.Xml).FirstOrDefault(); if (string.IsNullOrEmpty(xmlFileName)) { return; } // allow multiple xsd files as one schema var xsdFileNames = dialogService.AskForFiles(true, FileType.Xsd); if (string.IsNullOrEmpty(xsdFileNames.FirstOrDefault())) { return; } if (!File.Exists(xmlFileName) || xsdFileNames.Any(path => !File.Exists(path))) { throw new ArgumentException("One or more files not found"); } logger.Debug($"Try to validate {xmlFileName} by {string.Join(", ", xsdFileNames)}"); var errors = xsdValidator.Validate(xmlFileName, xsdFileNames); if (!errors.Any()) { dialogService.ShowInfo(UIStrings.SuccessfullyValidatedMessage); return; } logger.Debug($"{errors.Count} errors"); var ws = (Worksheet)excelApp.Workbooks.Add().Worksheets[1]; ((Range)ws.Cells[1, 1]).Value = UIStrings.XmlValidationReport_Severity; ((Range)ws.Cells[1, 2]).Value = UIStrings.XmlValidationReport_ElementName; ((Range)ws.Cells[1, 3]).Value = UIStrings.XmlValidationReport_Value; ((Range)ws.Cells[1, 4]).Value = UIStrings.XmlValidationReport_Message; var i = 2; foreach (var error in errors) { ((Range)ws.Cells[i, 1]).Value = error.Severity.ToString(); ((Range)ws.Cells[i, 2]).Value = error.ElementName; ((Range)ws.Cells[i, 3]).Value = error.Value; ((Range)ws.Cells[i, 4]).Value = error.Message; i++; } }
public void CreateSampleXml() { var xsdFileName = dialogService.AskForFiles(false, FileType.Xsd).FirstOrDefault(); if (string.IsNullOrEmpty(xsdFileName)) { logger.Error("Empty file path"); return; } var targetFileName = dialogService.AskFileNameSaveAs("sample.xml", FileType.Xml); if (string.IsNullOrEmpty(targetFileName)) { logger.Error("Empty target file path"); return; } var sampleGenerator = new XmlSampleGenerator(xsdFileName, XmlQualifiedName.Empty); using (var fileStream = new FileStream(targetFileName, FileMode.CreateNew)) using (var xmlTextWriter = new XmlTextWriter(fileStream, Encoding.UTF8)) { xmlTextWriter.Formatting = Formatting.Indented; sampleGenerator.WriteXml(xmlTextWriter); } }