We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Is it possible to replace a StructuredDocumentTag from one document to another?

I have two documents that are identical, however, I would like to replace/move the contents of a StructuredDocumentTag from one document to another. This will allow me to maintain the original text in the document and only update StructuredDocumentTag that has changed. Hopefully, that makes sense.

Appreciate any suggestions

@markchequer Sure, you can copy/replace content of SDT in one document with content from another document’s SDT. For example see the following code:

Document dst = new Document(@"C:\Temp\dst.docx");
Document src = new Document(@"C:\Temp\src.docx");

// Create NodeImporter to import nodes from one document into another.
NodeImporter importer = new NodeImporter(src, dst, ImportFormatMode.UseDestinationStyles);

// Get SDT from source and destination documents.
// For demonstration purposes the SDT is selected by it's tag.
StructuredDocumentTag dstTag = dst.GetChildNodes(NodeType.StructuredDocumentTag, true).Cast<StructuredDocumentTag>()
    .Where(sdt => sdt.Tag == "Data").FirstOrDefault();

StructuredDocumentTag srcTag = src.GetChildNodes(NodeType.StructuredDocumentTag, true).Cast<StructuredDocumentTag>()
    .Where(sdt => sdt.Tag == "Data").FirstOrDefault();

// Remove all children from the destimation SDT.

// Copy all children from source SDT into the destination SDT.
foreach(Node child in srcTag.ChildNodes)
    dstTag.AppendChild(importer.ImportNode(child, true));


Alexey, thank you. This is spot on my problem and mostly fixes the issue. However, when testing it seems to have an issue if there is a Paragraph as a child node.

@markchequer The problem might occur because your source and destination SDTs are on different levels. Please see StructuredDocumentTag.Level property.
Could you please attach your source and destination documents and specify what SDTs causes the problem?