Example #1
0
        private void OnPackageValidationError(Object sender, OpenXmlPackageValidationEventArgs e)
        {
            ValidationErrorInfo errorInfo = new ValidationErrorInfo();

            errorInfo.ErrorType = ValidationErrorType.Package;
            errorInfo.Id        = "Pkg_" + e.MessageId;

            string name;

            switch (errorInfo.Id)
            {
            case "Pkg_PartIsNotAllowed":
                Debug.Assert(e.SubPart != null);
                name = e.Part != null?GetPartNameAndUri(e.Part) : GetDocumentName(this.TargetDocument);

                errorInfo.Description = String.Format(CultureInfo.CurrentUICulture, ValidationResources.Pkg_PartIsNotAllowed, name, GetPartNameAndUri(e.SubPart));
                break;

            case "Pkg_RequiredPartDoNotExist":
                errorInfo.Description = string.Format(System.Globalization.CultureInfo.CurrentUICulture,
                                                      ValidationResources.Pkg_RequiredPartDoNotExist, e.PartClassName);
                break;

            case "Pkg_OnlyOnePartAllowed":
                name = e.Part != null?GetPartNameAndUri(e.Part) : GetDocumentName(this.TargetDocument);

                errorInfo.Description = String.Format(CultureInfo.CurrentUICulture, ValidationResources.Pkg_OnlyOnePartAllowed, name, e.PartClassName);
#if DEBUG
                Debug.Assert(e.SubPart != null);
                errorInfo.RelatedPart = e.SubPart;
#endif
                break;

            case "Pkg_ExtendedPartIsOpenXmlPart":
                Debug.Assert(e.SubPart != null);
                errorInfo.Description = string.Format(System.Globalization.CultureInfo.CurrentUICulture,
                                                      ValidationResources.Pkg_ExtendedPartIsOpenXmlPart, GetPartUri(e.SubPart));
                break;

            case "Pkg_DataPartReferenceIsNotAllowed":
                Debug.Assert(e.DataPartReferenceRelationship != null);
                name = e.Part != null?GetPartNameAndUri(e.Part) : GetDocumentName(this.TargetDocument);

                errorInfo.Description = String.Format(CultureInfo.CurrentUICulture, ValidationResources.Pkg_PartIsNotAllowed, name, e.DataPartReferenceRelationship.Uri);
                break;

            case "Pkg_InvalidContentTypePart":      // won't get this error.
            default:
                Debug.Assert(false, "Invalid package validation event.");
                break;
            }

            if (e.Part != null)
            {
                errorInfo.Part = e.Part;
                errorInfo.Path = new XmlPath(e.Part);
            }
            errorInfo.RelatedPart = e.SubPart;

            this.ValidationResult.AddError(errorInfo);
        }
        private void OnPackageValidationError(Object sender, OpenXmlPackageValidationEventArgs e)
        {            
            ValidationErrorInfo errorInfo = new ValidationErrorInfo();
            errorInfo.ErrorType = ValidationErrorType.Package;
            errorInfo.Id = "Pkg_" + e.MessageId;

            string name;

            switch (errorInfo.Id)
            {
                case "Pkg_PartIsNotAllowed":
                    Debug.Assert(e.SubPart != null);
                    name = e.Part != null ? GetPartNameAndUri(e.Part) : GetDocumentName(this.TargetDocument);
                    errorInfo.Description = String.Format(CultureInfo.CurrentUICulture, ValidationResources.Pkg_PartIsNotAllowed, name, GetPartNameAndUri(e.SubPart));
                    break;

                case "Pkg_RequiredPartDoNotExist":
                    errorInfo.Description = string.Format(System.Globalization.CultureInfo.CurrentUICulture,
                                                ValidationResources.Pkg_RequiredPartDoNotExist, e.PartClassName);
                    break;

                case "Pkg_OnlyOnePartAllowed":
                    name = e.Part != null ? GetPartNameAndUri(e.Part) : GetDocumentName(this.TargetDocument);
                    errorInfo.Description = String.Format(CultureInfo.CurrentUICulture, ValidationResources.Pkg_OnlyOnePartAllowed, name, e.PartClassName);
#if DEBUG
                    Debug.Assert(e.SubPart != null);
                    errorInfo.RelatedPart = e.SubPart;
#endif
                    break;

                case "Pkg_ExtendedPartIsOpenXmlPart":
                    Debug.Assert(e.SubPart != null);
                    errorInfo.Description = string.Format(System.Globalization.CultureInfo.CurrentUICulture,
                                                ValidationResources.Pkg_ExtendedPartIsOpenXmlPart, GetPartUri(e.SubPart));
                    break;

                case "Pkg_DataPartReferenceIsNotAllowed":
                    Debug.Assert(e.DataPartReferenceRelationship != null);
                    name = e.Part != null ? GetPartNameAndUri(e.Part) : GetDocumentName(this.TargetDocument);
                    errorInfo.Description = String.Format(CultureInfo.CurrentUICulture, ValidationResources.Pkg_PartIsNotAllowed, name, e.DataPartReferenceRelationship.Uri);
                    break;

                case "Pkg_InvalidContentTypePart":  // won't get this error.
                default:
                    Debug.Assert(false, "Invalid package validation event.");
                    break;
            }

            if (e.Part != null)
            {
                errorInfo.Part = e.Part;
                errorInfo.Path = new XmlPath(e.Part);
            }
            errorInfo.RelatedPart = e.SubPart;

            this.ValidationResult.AddError(errorInfo);
        }