dotnet / dotnet/Open-XML-SDK

CustomUI having wrong namespace

Open
#1,210 8 comments 0 reactions 1 assignee Claimed by @mikeebowen View on GitHub
bug
Dominant language
C#
Stars
4.6k
Forks
605
PR merge metrics
No merged PRs in 30d

Description

**Describe the bug**
I have a spreadsheet, which I get exception informing me the namespace is invalid. The exception thrown informs:

```
InvalidDataException: The root XML element "http://schemas.microsoft.com/office/2006/01/customui:customUI" in the part is incorrect. The expected root XML element is: "http://schemas.microsoft.com/office/2009/07/customui:customUI".
```

The original spreadsheet has wrong/invalid namespace, but after having used ChangeDocumentType from .xlsm to .xlsx, I think this namespace should have been resolved to the right namespace automatically by Open XML SDK even though the namespace is (what I think to be) not related to ChangeDocumentType. This could potentially be expanded to include that if I use ChangeDocumentType on any .xlsx to .xlsx then all invalid namespaces would be resolved.

The namespace is found in root folder ```/customUI``` and any ```customUI.xml``` files in the folder.

I am trying to make my own fix with the below code, BUT the code is not working and the code will not even write any namespace to console (if I add a Console.WriteLine(name.Value) line to the code). I do know ```spreadsheet.RibbonAndBackstageCustomizationsPart``` is not null, if you write anything to console immediately after the ``{`` in the first ``if`` statement. If I use the below code as part of ChangeDocumentType from .xlsm to .xlsx, then the conversion will fail, which is strange, since the conversion will even fail if I do not try to change any namespaces but if I only try to write the namespaces to console it fails.

```csharp
using (SpreadsheetDocument spreadsheet = SpreadsheetDocument.Open(output_filepath, true))
{
if (spreadsheet.RibbonAndBackstageCustomizationsPart != null)
{
var namespaces = spreadsheet.RibbonAndBackstageCustomizationsPart.CustomUI.NamespaceDeclarations.ToList();
foreach (var name in namespaces)
{
if (name.Value.Equals("http://schemas.microsoft.com/office/2006/01/customui:customUI"))
{
spreadsheet.RibbonAndBackstageCustomizationsPart.CustomUI.RemoveNamespaceDeclaration("x");
spreadsheet.RibbonAndBackstageCustomizationsPart.CustomUI.AddNamespaceDeclaration("x", "http://schemas.microsoft.com/office/2009/07/customui:customUI");
}
}
}
}
```

**Observed behavior**
Wrong namespace in customUI.xml
```http://schemas.microsoft.com/office/2006/01/customui:customUI```

also does
```spreadsheet.RibbonAndBackstageCustomizationsPart.CustomUI``` work?

**Expected behavior**
Right namespace in customUI.xml
```http://schemas.microsoft.com/office/2009/07/customui:customUI```
after having used ChangeDocumentType, this function should update any invalid namespaces to valid namespaces.

also

```spreadsheet.RibbonAndBackstageCustomizationsPart.CustomUI.NamespaceDeclarations``` should report any namespaces

**Desktop (please complete the following information):**
- OS: Windows 11
- Office version 2208
- .NET Target: .Net Core 6.0.9
- DocumentFormat.OpenXml Version: 2.18.0

**Additional context**
See data sample: https://github.com/Asbjoedt/CLISC/blob/master/Docs/Example3.xlsm

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.