private void lstErrors_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ValidationIssue vi = _lstErrors.SelectedItem as ValidationIssue;

            if (vi != null && vi.Line != 0)
            {
                int offset = _editor.XmlEditor.Document.GetOffset(vi.Line, vi.Column);
                _editor.XmlEditor.CaretOffset = offset;
                _editor.XmlEditor.ScrollToLine(vi.Line);
                _editor.XmlEditor.Focus();
            }
        }
Beispiel #2
0
        private void settings_ValidationEventHandler(object sender, ValidationEventArgs e)
        {
            var ex = e.Exception as XmlSchemaValidationException;

            if (ex != null)
            {
                if (string.IsNullOrEmpty(ex.SourceUri))
                {
                    var vi = new ValidationIssue(ValidationIssue.Type.Error, ex.LineNumber, ex.LinePosition, e.Message);
                    _result.Results.Add(vi);
                }
                else
                {
                    var vi = new ValidationIssue(ValidationIssue.Type.Error, ex.LineNumber, ex.LinePosition, ex.SourceUri, e.Message);
                    _result.Results.Add(vi);
                }
            }
            else
            {
                var vi = new ValidationIssue(ValidationIssue.Type.Error, 0, 0, e.Message);
                _result.Results.Add(vi);
            }
            _result.State = ValidationState.ValidationError;
        }
        void validationUpdateTimer_Tick(object sender, EventArgs e)
        {
            Debug.Assert(_editor != null);

            _lstErrors.Items.Clear();
            _errorMarker.ClearMarks();

            try
            {
                if (_validationData.DoNotValidate)
                {
                    var i1 = new ValidationIssue(ValidationIssue.Type.Information, 0, 0, "Validation is turned off.\nTo activate, click in ribbon on 'Validation' tab and select 'Check wellformedness' or 'Validate against schema'.");
                    _lstErrors.Items.Add(i1);
                }

                else if (_validationData.CheckWellformedness)
                {
                    try
                    {
                        XmlDocument xmlDoc = _editor.XmlEditor.Text.ToXmlDocument();  //TODO make async
                        var         i      = new ValidationIssue(ValidationIssue.Type.Information, 0, 0, "XML is wellformed");
                        _lstErrors.Items.Add(i);
                    }
                    catch (XmlException ex)
                    {
                        var i = new ValidationIssue(ValidationIssue.Type.Error, ex.LineNumber, ex.LinePosition,
                                                    ex.Message);
                        _lstErrors.Items.Add(i);
                    }
                }

                else if (_validationData.CheckXsd)
                {
                    string xsdFile = _validationData.Xsd;
                    if (!FileHelper.FileExists(xsdFile))
                    {
                        var i = new ValidationIssue(ValidationIssue.Type.Warning, 0, 0,
                                                    $"The file '{xsdFile}' does not exist, or the path is invalid");
                        _lstErrors.Items.Add(i);
                        return;
                    }

                    XsdValidationResult result = XsdValidationHelper.Instance.ValidateInstance(
                        xsdFile, _editor.XmlEditor.Text);
                    if (result.Results.Count == 0)
                    {
                        var i = new ValidationIssue(ValidationIssue.Type.Information, 0, 0, "XML is valid");
                        _lstErrors.Items.Add(i);
                    }
                    else
                    {
                        foreach (var x in result.Results)
                        {
                            _lstErrors.Items.Add(x);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                var i = new ValidationIssue(ValidationIssue.Type.Error, 0, 0, "Error while validating file: " + ex.Message);
                _lstErrors.Items.Add(i);
            }

            foreach (var item in _lstErrors.Items)
            {
                var error = item as ValidationIssue;
                if (error != null)
                {
                    if (error.IssueType != ValidationIssue.Type.Information)
                    {
                        int offset = error.Line == 0 ? 0 : _editor.XmlEditor.Document.GetOffset(error.Line, error.Column);
                        _errorMarker.AddOffsetToMark(new MarkBackgroundRenderer.Mark
                        {
                            Offset = offset,
                            Length = 1,
                            Brush  = new SolidColorBrush(Color.FromArgb(0x40, 0xFF, 0, 0))
                        });
                    }
                }
            }
        }
Beispiel #4
0
        public XsdValidationResult ValidateInstance(XmlTextReader xsdReader, string instanceDoc)
        {
            _result = new XsdValidationResult();
            try
            {
                XmlReaderSettings settings = new XmlReaderSettings();
                settings.ValidationType = ValidationType.Schema;
                //settings.ValidationFlags = XmlSchemaValidationFlags.ProcessSchemaLocation;
                try
                {
                    settings.Schemas.Add(null, xsdReader);
                }
                catch (Exception ex)
                {
                    throw new Exception("Invalid schema file: " + ex.Message, ex);
                }
                settings.ValidationEventHandler += settings_ValidationEventHandler;
                List <string> list = new List <string>();
                foreach (XmlSchema schema in settings.Schemas.Schemas())
                {
                    if (!string.IsNullOrEmpty(schema.TargetNamespace))
                    {
                        list.Add(schema.TargetNamespace);
                    }
                }

                XmlReader reader2 = XmlReader.Create(instanceDoc.ToStream(), settings);
                while (reader2.Read())
                {
                }
                if (_result.State == ValidationState.Success)
                {
                    XmlDocument xd = instanceDoc.ToXmlDocument();
                    if (!list.Contains(xd.DocumentElement.NamespaceURI))
                    {
                        StringBuilder builder = new StringBuilder();

                        builder.AppendLine("The namespace of the current document does not match any of the target namespaces for the loaded schemas.");
                        builder.AppendLine("Have you specified the correct schema for the current document?");
                        builder.AppendLine("Document Namespace:\t");
                        builder.AppendLine(xd.DocumentElement.NamespaceURI);
                        builder.AppendLine("Schema Target Namespace(s):");

                        if (list.Count == 0)
                        {
                            builder.Append("\t(None)");
                        }
                        else
                        {
                            foreach (string t in list)
                            {
                                builder.Append("\t");
                                builder.AppendLine(t);
                            }
                        }
                        var vi = new ValidationIssue(ValidationIssue.Type.Information, 0, 0, builder.ToString());
                        _result.Results.Add(vi);

                        _result.State = ValidationState.Warning;
                    }
                }
            }
            catch (XmlSchemaException exception)
            {
                string text = string.Format("Invalid xsd file ({0}:{1}): {2}", exception.LineNumber, exception.LinePosition, exception.Message);
                var    vi   = new ValidationIssue(ValidationIssue.Type.Error, 0, 0, text);
                _result.Results.Add(vi);
                _result.State = ValidationState.OtherError;
            }
            catch (XmlException exception)
            {
                var vi = new ValidationIssue(ValidationIssue.Type.Error, exception.LineNumber, exception.LinePosition, exception.Message);
                _result.Results.Add(vi);
                _result.State = ValidationState.OtherError;
            }
            catch (Exception exception)
            {
                var vi = new ValidationIssue(ValidationIssue.Type.Error, 0, 0, exception.Message);
                _result.Results.Add(vi);
                _result.State = ValidationState.OtherError;
            }
            finally
            {
                if (xsdReader != null)
                {
                    xsdReader.Close();
                }
            }
            return(_result);
        }