コード例 #1
0
#pragma warning disable 0618 // CS0618: A class member was marked with the Obsolete attribute, such that a warning will be issued when the class member is referenced.

        private void ValidatePackageStructure(OpenXmlPackage document)
        {
            OpenXmlPackageValidationSettings packageValidationSettings = new OpenXmlPackageValidationSettings();

            packageValidationSettings.EventHandler += this.OnPackageValidationError;

            document.Validate(packageValidationSettings, this.ValidationSettings.FileFormat);

            return;
        }
コード例 #2
0
#pragma warning disable 0618 // CS0618: A class member was marked with the Obsolete attribute, such that a warning will be issued when the class member is referenced.

        private void ValidatePackageStructure(OpenXmlPackage document)
        {
            OpenXmlPackageValidationSettings packageValidationSettings = new OpenXmlPackageValidationSettings();

            packageValidationSettings.EventHandler += this.OnPackageValidationError;

            document.Validate(packageValidationSettings, this.ValidationSettings.FileFormat);
            
            return;
        }
コード例 #3
0
        private void ValidatePackageStructure(OpenXmlPackage document, ValidationContext context)
        {
            var documentName = document.GetType().Name;

#pragma warning disable CS0618 // Type or member is obsolete
            var packageValidationSettings = new OpenXmlPackageValidationSettings();
#pragma warning restore CS0618 // Type or member is obsolete

            packageValidationSettings.EventHandler += OnPackageValidationError;

            document.Validate(packageValidationSettings, _validationSettings.FileFormat);

#pragma warning disable CS0618 // Type or member is obsolete
            void OnPackageValidationError(Object sender, OpenXmlPackageValidationEventArgs e)
#pragma warning restore CS0618 // Type or member is obsolete
            {
                var errorInfo = new ValidationErrorInfo
                {
                    ErrorType = ValidationErrorType.Package,
                    Id        = "Pkg_" + e.MessageId
                };

                string name;

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

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

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

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

                    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(CultureInfo.CurrentUICulture, ValidationResources.Pkg_ExtendedPartIsOpenXmlPart, GetPartUri(e.SubPart));
                    break;

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

                    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;

                context.AddError(errorInfo);
            }
        }