question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

OpenXML SDK will throw exception when a percentage with % char

See original GitHub issue

Before submitting an issue, please fill this out

Is this a:

  • Issue with the OpenXml library

Description

When the document contains % characters, I use the OpenXML SDK to read a percentage value, an exception will be thrown.

Information

  • .NET Target: All
  • DocumentFormat.OpenXml Version: 2.12.0

https://github.com/lindexi/lindexi_gd/tree/9b506dea/RurlejileGearhuheljale

                    var background = slide.CommonSlideData.Background;
                    var backgroundProperties = background.BackgroundProperties;
                    var solidFill = backgroundProperties.GetFirstChild<SolidFill>();
                    var solidFillRgbColorModelHex = solidFill.RgbColorModelHex;
                    var alpha = solidFillRgbColorModelHex.GetFirstChild<Alpha>();
                    try
                    {
                        int alphaVal = alpha.Val;
                    }
                    catch (Exception e)
                    {
                        // Input string was not in a correct format.
                        Console.WriteLine(e);
                    }

The document:

    <p:bg>
      <p:bgPr>
        <a:solidFill>
          <a:srgbClr val="383747">
            <a:alpha val="100.00%" />
          </a:srgbClr>
        </a:solidFill>
      </p:bgPr>
    </p:bg>

Observed

The code throw exception

int alphaVal = alpha.Val;

Expected

It can convert to int value. We can convert 100% to 100000 value

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
tomjebocommented, Aug 24, 2022

@adamshakhabov If you look at our implementer notes for the ISO 29500 standard (equivalent to ECMA 376), here:

MS-OI29500 2.1.1327 Part 1 Section 20.1.10.45 ST_PositiveFixedPercentage (Positive Fixed Percentage)

It says:

2.1.1327 Part 1 Section 20.1.10.45 ST_PositiveFixedPercentage (Positive Fixed Percentage) a. The standard states that ST_PositiveFixedPercentage is read and written as a percent with a trailing percent sign. Office will read percentages formatted with a trailing percent sign or formatted as 1000th of a percent without a trailing percent sign, but only write percentages as 1000th’s of a percent without a trailing percent sign.

This at least explains Office behavior here.

@lindexi Regarding the alpha class, it does look like it should get and set the Val property as an int. We will investigate this to handle the conversion from the string form.

1reaction
ashahabovcommented, Feb 1, 2021

Maybe such string (input="100.00%") should be pre-processed here before passing into XmlConvert.ToInt32():

Int32Value.cs

...
private protected override int Parse(string input) => XmlConvert.ToInt32(input);
...
Read more comments on GitHub >

github_iconTop Results From Across the Web

OpenXML SDK will throw exception when a percentage ...
Description. When the document contains % characters, I use the OpenXML SDK to read a percentage value, an exception will be thrown.
Read more >
OpenXML SDK returning a number for CellValue instead of ...
Amurra's answer seems to go ninety percent of the way, but it may need some nuance. 1) The function "GetSharedStringItemById" returns a ...
Read more >
XmlDocument.Load() => XmlException at "percent" char
This is an encoding problem. You can work around it by using WebClient , like this: private void ParseXML(string url) { string xmlSource; ......
Read more >
Create Excel using OpenXML in .NET 6 | by Jason Ge - Medium
In order to export data into Excel format, we can use OpenXML to achieve it ... <exception cref="ArgumentNullException">Thrown when "path" is null reference ......
Read more >
Understand when and how to use Office Open XML in your ...
Overview of when and how to use Office Open XML in your Word add-in.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found