Skip to content

Misc element properties are not lining up with their ECMA definitions #780

Open
@rmboggs

Description

@rmboggs

Before submitting an issue, please fill this out

Is this a:

  • Issue with the OpenXml library
  • Question on library usage

Description

The DocumentFormat.OpenXml.WordProcessing.OnOffOnlyValues enum only contains values On and Off but if I am reading the ECMA-376 standard correctly, properties with this value type may contain values On, Off, 0, 1, true, and false. When values, such as true or false, are present for an xml attribute for this type, the Value property of the EnumValue<OnOffOnlyType> object throws a System.FormatException exception.

The attached document contains an element /document[0]/tbl[0]/tr[0]/trPr[0]/cantSplit which contains the property in question.
file-sample_500kB.docx

I'm open to discuss this further but it doesn't seem like this should be the correct behavior.

Information

  • .NET Target: .NET Framework 4.7.2
  • DocumentFormat.OpenXml Version: 2.11.3

Repro

using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using System;
using System.IO;
using System.Linq;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            // Change  to path of test file
            const string pathToFile = @"C:\path\to\file-sample_500kB.docx";

            using (var f = new FileStream(pathToFile, FileMode.Open))
            {
                using (var docx = WordprocessingDocument.Open(f, false))
                {
                    var sample = docx.MainDocumentPart.Document.Descendants<CantSplit>().FirstOrDefault();

                    if (sample != null)
                    {
                        var val = sample.Val;
                        Console.WriteLine($"Sample has value: {val.HasValue}");
                        Console.WriteLine($"Sample inner text: {val.InnerText}");

                        try
                        {
                            Console.WriteLine($"Sample value: {val.Value}");
                        }
                        catch (Exception x)
                        {
                            Console.WriteLine($"Sample value threw {x.GetType().Name}");
                            Console.WriteLine($"Sample value exception message: {x.Message}");
                        }
                    }
                }
            }
            Console.WriteLine("Press any key to quit");
            Console.ReadKey(true);
        }
    }
}

Observed

Sample has value: False
Sample inner text: false
Sample value threw FormatException
Sample value exception message: The text value is not a valid enumeration value.
Press any key to quit

Expected

Sample has value: True
Sample inner text: false
Sample value: DocumentFormat.OpenXml.Wordprocessing.OnOffOnlyValues.False or DocumentFormat.OpenXml.Wordprocessing.OnOffOnlyValues.Off
Press any key to quit

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions