Ejemplo n.º 1
0
        public IActionResult Index(XMLandSchemaFileUpload xmlAndSchemaFiles)
        {
            if (xmlAndSchemaFiles == null)
            {
                return(NotFound());
            }

            IFormFile schemaFile = xmlAndSchemaFiles.SchemaFile;
            IFormFile xmlFile    = xmlAndSchemaFiles.XmlFile;

            if (schemaFile == null)
            {
                ModelState.AddModelError("SchemaFile", "A schema file is required");
            }
            if (xmlFile == null)
            {
                ModelState.AddModelError("XmlFile", "A XML file is required");
            }

            if (ModelState.IsValid)
            {
                XmlReader schemaFileReader = XmlReader.Create(schemaFile.OpenReadStream());

                XmlSchemaSet sc = new XmlSchemaSet();
                sc.Add(null, schemaFileReader);

                XmlReaderSettings settings = new XmlReaderSettings();
                settings.ValidationType = ValidationType.Schema;
                settings.Schemas        = sc;

                List <XmlValidationError> validationResults = new List <XmlValidationError>();
                settings.ValidationEventHandler +=
                    (s, e) => validationResults.Add(new XmlValidationError
                {
                    Element   = ((XmlReader)s).Name,
                    ErrorType = e.Severity.ToString(),
                    Line      = e.Exception.LineNumber,
                    Column    = e.Exception.LinePosition,
                    Message   = e.Message
                });

                XmlReader xmlFileReader = XmlReader.Create(xmlFile.OpenReadStream(), settings);
                while (xmlFileReader.Read())
                {
                }

                ViewBag.XmlFileName    = xmlFile.FileName;
                ViewBag.SchemaFileName = schemaFile.FileName;

                return(View("ValidationResult", validationResults));
            }
            else
            {
                return(View());
            }
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Index(XMLandSchemaFileUpload xmlAndSchemaFiles)
        {
            if (ModelState.IsValid)
            {
                IFormFile schemaFile = xmlAndSchemaFiles.SchemaFile;
                IFormFile xmlFile    = xmlAndSchemaFiles.XmlFile;


                ViewData["XMLFILE"]    = xmlFile.FileName;
                ViewData["SCHEMAFILE"] = schemaFile.FileName;

                XmlReaderSettings settings     = new XmlReaderSettings();
                XmlReader         schemaReader = XmlReader.Create(schemaFile.OpenReadStream());

                XmlSchemaSet sc = new XmlSchemaSet();
                sc.Add(null, schemaReader);


                settings.ValidationType = ValidationType.Schema;
                settings.Schemas        = sc;


                List <XmlValidationError> validationResults = new List <XmlValidationError>();

                settings.ValidationEventHandler +=
                    (s, e) => validationResults.Add(new XmlValidationError
                {
                    Element   = ((XmlReader)s).Name,
                    ErrorType = e.Severity.ToString(),
                    Line      = e.Exception.LineNumber,
                    Column    = e.Exception.LinePosition,
                    Message   = e.Message
                });
                XmlReader xmlReader = XmlReader.Create(xmlFile.OpenReadStream(), settings);

                while (xmlReader.Read())
                {
                }
                //xmlReader.Close();
                return(View("ValidationResult", validationResults));
            }
            //return View("ValidationResult", validationResults);
            return(View());
        }