Insert Content Control in Word DOCX & Bind it to Custom XML Part C# | Migrate OpenXML Code to Aspose.Words for .NET

We are advised to migrate our legacy code from OpenXML to Aspose.Words, but after reviewing the product, i see the customXMLPart is not 100% compatible with OpenXML.
I have this working OpenXML code, and needed to be migrated to Aspose.Words,
OpenXML code generates a document that i can see the xml content is inserted inside the document. but using Aspose.Words, i do not see the xml content is inside the document.

using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(templateFile, true))
{        wordDoc.ChangeDocumentType(DocumentFormat.OpenXml.WordprocessingDocumentType.Document);
    MainDocumentPart mainPart = wordDoc.MainDocumentPart;

    mainPart.DeleteParts<CustomXmlPart>(mainPart.CustomXmlParts);

    // Add a new customXML part and then add content
    CustomXmlPart customXmlPart = mainPart.AddCustomXmlPart(CustomXmlPartType.CustomXml);

    // Copy the XML into the new part
    using (StreamWriter ts = new StreamWriter(customXmlPart.GetStream()))
        ts.Write(customXML);
    mainPart.Document.Save();
}

here the Aspose.Words code ->

var myxmlPart = new Aspose.Words.Markup.CustomXmlPart();
string sXML = customXML.ToString();
myxmlPart.Data = System.Text.Encoding.ASCII.GetBytes(sXML);
myxmlPart.Id = Guid.NewGuid().ToString().ToUpper();
// xpc.Clear();
doc.CustomXmlParts.Add(myxmlPart);
Aspose.Words.Markup.StructuredDocumentTag tag = new Aspose.Words.Markup.StructuredDocumentTag(doc, Aspose.Words.Markup.SdtType.PlainText,
    Aspose.Words.Markup.MarkupLevel.Block);
//tag.XmlMapping.SetMapping(myxmlPart,"/root",string.Empty);
// doc.FirstSection.Body.AppendChild(tag);
string sfile = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(templateFile), "11111.docx");
doc.Save(sfile);

@nahedkadih,

Can you please also ZIP and attach the following resources here for testing?

  • A simplified input Word document
  • Aspose.Words for .NET 20.10 generated output DOCX file showing the undesired behavior
  • Your expected DOCX file showing the desired output. You can create this document by using OpenXML SDK.

As soon as you get these pieces of information ready, we will start further investigation into your particular scenario/issue and provide you code to achieve the same by using Aspose.Words.

@nahedkadih,

Please check the following code example:

public void OpenXml()
{
    Document doc = new Document();

    CustomXmlPart myxmlPart = new CustomXmlPart();
    const string sXml = "<root><text>Text element #1</text><text>Text element #2</text></root>";
    myxmlPart.Data = Encoding.ASCII.GetBytes(sXml);
    myxmlPart.Id = Guid.NewGuid().ToString().ToUpper();

    doc.CustomXmlParts.Clear();

    doc.CustomXmlParts.Add(myxmlPart);

    StructuredDocumentTag tag = new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Block);
    tag.XmlMapping.SetMapping(myxmlPart, "/root[1]/text[1]", string.Empty); // or "/root[1]/text[2]" to set 'Text element #2'
    doc.FirstSection.Body.AppendChild(tag);

    doc.Save("ReworkOpenXml.docx");
} 

P.S. We are unable to completely understand your requirement. If the above code is not helpful then we request you to please elaborate your inquiry further and provide the resources requested in my previous post. This will help us to understand your scenario, and we will be in a better position to address your concerns accordingly.