Navigate trought multiple levels of sdts to set a value:

Hello

I’m using the code below to set value on the StructuredDocumentTag i want, and it works fine for the simple elements on the example file below, but it doesn’t work for the ones nested in the table type SDTs.

private void updateValue(StructuredDocumentTag sdt, String value) {
		// remove the text from the sdt and remember a reference node
		CompositeNode<?> referenceNode = null;
		for (Object node : sdt.getChildNodes(NodeType.RUN, true)) {
			referenceNode = ((Run)node).getParentNode();
			((Run)node).remove();
		}

		// add the updated value back to the reference node
		if (referenceNode != null) {
			Run updatedRun = new Run(sdt.getDocument(), value);
			referenceNode.appendChild(updatedRun);
		}
	}

schema.7z (3.2 KB)

How could i adapt the code to work with these tables, or is there a better way do achieve replacing the sdt content text?

Thank you.

@lconde57 Could you please attach your input document here for testing? We will check the issue and provide you more information.

Thank you, this is the file of the xml above: Table Example.docx (18.4 KB)

@lconde57 In your case repeating section is used in your table. To edit it’s data it is required to edit XML part the data is binded from. For example, here is simple code that shows a simple example:

Document doc = new Document(@"C:\Temp\in.docx");

// Get XML part.
CustomXmlPart part = doc.CustomXmlParts[1];

// Load data into XML document.
XmlDocument xmlData = new XmlDocument();
xmlData.Load(new MemoryStream(part.Data));

// Edit data in the XML document.
XmlNamespaceManager namespaceManager = new XmlNamespaceManager(xmlData.NameTable);
namespaceManager.AddNamespace("sqph", "http://schemas.sciquest.com/tcm/office/placeholders/v1");
XmlNode rowsRoot = xmlData.SelectSingleNode("/sqph:contractplaceholders/sqph:UDF_29722", namespaceManager);

// Clone the rows and chnage the data.
XmlNode row = rowsRoot.ChildNodes[0].CloneNode(true);

// Remove old rows
while (rowsRoot.HasChildNodes)
    rowsRoot.RemoveChild(rowsRoot.ChildNodes[0]);

// Add several rows.
for (int i = 0; i < 10; i++)
{
    XmlNode newRow = row.CloneNode(true);
    int cellIdx = 0;
    foreach (XmlNode cell in newRow.ChildNodes)
    {
        cell.InnerText = string.Format("{0}{1}", i, cellIdx);
        cellIdx++;
    }
    rowsRoot.AppendChild(newRow);
}

// Save edited XML into stream and reset CustomXmlPart.Data with it.
using (MemoryStream dataStream = new MemoryStream())
{
    xmlData.Save(dataStream);
    part.Data = dataStream.ToArray();
}

doc.Save(@"C:\Temp\out.docx");

Here is the output document produced by this code: out.docx (17.0 KB)