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.

CustomXmlPart incorrectly created as "customXML" rather than "customXml"

See original GitHub issue

Describe the bug When I create a new CustomXmlPart in an existing document it is created in β€œ./customXML/item[id].xml” whereas word created the directory as β€œ./customXml” rendering the document incomplete.

Steps to reproduce the behavior:


var xmlPart =  """
<?xml version="1.0" encoding="UTF-8"?>
  <test:testCustom xmlns:test="http://testag.ch/testCustom">
    <test:CMX_LoremIpsum>
      <test:Para1>Lorem Ipsum</test:Para1>
      <test:Para2>Dolor sit amet</test:Para2>
      <test:Para3>Consectetur adipiscing elit</test:Para3>
      <test:Para4>den Rest mΓΌsste ich googlen</test:Para4>
    </test:CMX_LoremIpsum>
    <test:CMX_DummyTexts>
      <test:Text1>Some text for Field 'Text1'</test:Text1>
      <test:Text2>(γƒŽΰ² η›Šΰ² )γƒŽε½‘β”»β”β”»</test:Text2>
    </test:CMX_DummyTexts>
  </test:testCustom>
""";

var part = Document.GetOrCreateCmiCustomXmlPart();
using var partStream = part.GetStream(FileMode.Create,
                                              FileAccess.ReadWrite);

if (partStream.CanSeek)
            partStream.Seek(0, SeekOrigin.Begin);

var doc = XDocument.Parse(xmlPart);

using var xmlWriter = XmlWriter.Create(partStream);
doc.Save(xmlWriter);



    public static CustomXmlPart GetOrCreateCustomXmlPart(this WordprocessingDocument wordDoc,
                                                         string partId)
    {
        wordDoc.MainDocumentPart!.TryGetPartById(partId,
                                                 out var found);
        var part = found as CustomXmlPart;

        part ??= wordDoc.MainDocumentPart!.AddCustomXmlPart(CustomXmlPartType.CustomXml,
                                                            partId);

        var customXmlProps = part.CustomXmlPropertiesPart ?? part.AddNewPart<CustomXmlPropertiesPart>();
        customXmlProps.DataStoreItem ??= new DataStoreItem() { ItemId = Guid.NewGuid().ToString("B") };

        return part;
    }

Observed behavior AddCustomXmlPart appears to create a new directory customXML:

β”œβ”€β”€ [Content_Types].xml
β”œβ”€β”€ customXml
β”‚Β Β  β”œβ”€β”€ item1.xml
β”‚Β Β  β”œβ”€β”€ item2.xml
β”‚Β Β  β”œβ”€β”€ item3.xml
β”‚Β Β  β”œβ”€β”€ item4.xml
β”‚Β Β  β”œβ”€β”€ itemProps1.xml
β”‚Β Β  β”œβ”€β”€ itemProps2.xml
β”‚Β Β  β”œβ”€β”€ itemProps3.xml
β”‚Β Β  β”œβ”€β”€ itemProps4.xml
β”‚Β Β  └── _rels
β”‚Β Β      β”œβ”€β”€ item1.xml.rels
β”‚Β Β      β”œβ”€β”€ item2.xml.rels
β”‚Β Β      β”œβ”€β”€ item3.xml.rels
β”‚Β Β      └── item4.xml.rels
β”œβ”€β”€ customXML
β”‚Β Β  β”œβ”€β”€ item5.xml
β”‚Β Β  β”œβ”€β”€ itemProps5.xml
β”‚Β Β  └── _rels
β”‚Β Β      └── item5.xml.rels
β”œβ”€β”€ docProps
β”‚Β Β  β”œβ”€β”€ app.xml
β”‚Β Β  β”œβ”€β”€ core.xml
β”‚Β Β  └── custom.xml
β”œβ”€β”€ _rels
└── word
    β”œβ”€β”€ document.xml
    β”œβ”€β”€ fontTable.xml
    β”œβ”€β”€ glossary
    β”‚Β Β  β”œβ”€β”€ document.xml
    β”‚Β Β  β”œβ”€β”€ fontTable.xml
    β”‚Β Β  β”œβ”€β”€ _rels
    β”‚Β Β  β”‚Β Β  └── document.xml.rels
    β”‚Β Β  β”œβ”€β”€ settings.xml
    β”‚Β Β  β”œβ”€β”€ styles.xml
    β”‚Β Β  └── webSettings.xml
    β”œβ”€β”€ _rels
    β”‚Β Β  └── document.xml.rels
    β”œβ”€β”€ settings.xml
    β”œβ”€β”€ styles.xml
    β”œβ”€β”€ theme
    β”‚Β Β  └── theme1.xml
    └── webSettings.xml

12 directories, 32 files

Expected behavior The new files should be created within customXml

.
β”œβ”€β”€ [Content_Types].xml
β”œβ”€β”€ customXml
β”‚Β Β  β”œβ”€β”€ item1.xml
β”‚Β Β  β”œβ”€β”€ item2.xml
β”‚Β Β  β”œβ”€β”€ item3.xml
β”‚Β Β  β”œβ”€β”€ item4.xml
β”‚Β Β  β”œβ”€β”€ item5.xml
β”‚Β Β  β”œβ”€β”€ itemProps1.xml
β”‚Β Β  β”œβ”€β”€ itemProps2.xml
β”‚Β Β  β”œβ”€β”€ itemProps3.xml
β”‚Β Β  β”œβ”€β”€ itemProps4.xml
β”‚Β Β  β”œβ”€β”€ itemProps5.xml
β”‚Β Β  └── _rels
β”‚Β Β      β”œβ”€β”€ item1.xml.rels
β”‚Β Β      β”œβ”€β”€ item2.xml.rels
β”‚Β Β      β”œβ”€β”€ item3.xml.rels
β”‚Β Β      β”œβ”€β”€ item4.xml.rels
β”‚Β Β      └── item5.xml.rels
β”œβ”€β”€ docProps
β”‚Β Β  β”œβ”€β”€ app.xml
β”‚Β Β  β”œβ”€β”€ core.xml
β”‚Β Β  └── custom.xml
β”œβ”€β”€ _rels
└── word
    β”œβ”€β”€ document.xml
    β”œβ”€β”€ fontTable.xml
    β”œβ”€β”€ glossary
    β”‚Β Β  β”œβ”€β”€ document.xml
    β”‚Β Β  β”œβ”€β”€ fontTable.xml
    β”‚Β Β  β”œβ”€β”€ _rels
    β”‚Β Β  β”‚Β Β  └── document.xml.rels
    β”‚Β Β  β”œβ”€β”€ settings.xml
    β”‚Β Β  β”œβ”€β”€ styles.xml
    β”‚Β Β  └── webSettings.xml
    β”œβ”€β”€ _rels
    β”‚Β Β  └── document.xml.rels
    β”œβ”€β”€ settings.xml
    β”œβ”€β”€ styles.xml
    β”œβ”€β”€ theme
    β”‚Β Β  └── theme1.xml
    └── webSettings.xml

10 directories, 32 files

Desktop (please complete the following information):

  • OS: Windows Server 2022 / Fedora 37
  • Office version -
  • .NET Target: .NET Framework 4.8
  • DocumentFormat.OpenXml Version: 2.19.0

Additional context Maybe it could be narrowed down to this: https://github.com/dotnet/Open-XML-SDK/blob/d46f92b2963f1c21415be08fb21532306e55db75/test/DocumentFormat.OpenXml.Packaging.Tests/data/PartConstraintData.json#L281

Issue Analytics

  • State:closed
  • Created 7 months ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
SimonSchwendelecommented, Mar 1, 2023

But perhaps we should change this to be consistent with Word.

It might prevent some confusion. Regarding rendering incomplete it appears that both docx files I used to test had broken bindings which is why they were displayed incomplete

0reactions
tomjebocommented, Mar 1, 2023

@mikeebowen if you’re going to make a change in the schema processing for #1305, perhaps you can throw this one in at the same time?

Read more comments on GitHub >

github_iconTop Results From Across the Web

C# using OpenXML to replace CustomXml not working
The following is the code I'm using to create a new word document from an existing document. What does work is that it...
Read more >
Custom XML part is not updated Β· Issue #1643
We can create a custom XML part via customXmlParts.addAsync and then update it. Current Behavior. We cannot updated a custom XML part onceΒ ......
Read more >
Custom XML mapped by Aspose.Word removed after ...
I have a problem with mapping custom xml in my worddocument. Everything works fine at first, im creating a StructuredDocumentTag, adding my custom...
Read more >
Cannot Bind Content Control to Custom XML - Run-time ...
I have a building block that contains Content Controls. When the Building Block is inserted I want to create a new Custom XML...
Read more >
Using custom XML parts in Word add-ins
The name says it all. This method adds a custom XML part to a Word document. ... The method takes the passed Word...
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