How to get HTML of SDT tag?

Hii Team,
I am trying to get HTML of SDT node.

Snippet:

var sdts = doc.GetChildNodes(NodeType.StructuredDocumentTag, true)

@AlpeshChaudhari12345 can you please attach your source Word document for reference?

@AlpeshChaudhari12345 You can use Node.ToString method to convert any node in the document to HTML:

// Get Some SDT.
Node sdt = doc.GetChild(NodeType.StructuredDocumentTag, 0, true);

// Conver it to HTML
Console.WriteLine(sdt.ToString(SaveFormat.Html));

FYI @eduardo.canal

1 Like

can i get whole html of sdt node with id of sdt ?
i am using this script to get specific sdt node.

Snippet

var sdts = doc.GetChildNodes(NodeType.StructuredDocumentTag, true).Where(x => ((Aspose.Words.Markup.StructuredDocumentTag)x).Tag.StartsWith("Rangescop_"));

SDTHTML.docx (4.2 MB)

@AlpeshChaudhari12345

can i get whole html of sdt node with id of sdt ?

Can you please be more specific about the expected result, will be helpful if you add an example of the expected result.
But if you meant to have the id of the SDT as the id of the HTML element, you can do it using the following code:

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

var sdts = doc.GetChildNodes(NodeType.StructuredDocumentTag, true).Where(x => ((StructuredDocumentTag)x).Tag.StartsWith("Rangescop_"));
foreach(Node sdt in sdts)
{
    var html = sdt.ToString(SaveFormat.Html);
    var indexTagBody = html.IndexOf(">", StringComparison.InvariantCultureIgnoreCase);
    // It is not really necessary, but just in case
    var indexEndInineTag = html.IndexOf("/>", StringComparison.InvariantCultureIgnoreCase);
    var indexForId = (indexEndInineTag > 0 && indexTagBody > indexEndInineTag) ? indexEndInineTag : indexTagBody;

    html = html.Insert(indexForId, $" id=\"{((StructuredDocumentTag)sdt).Id}\" ");
    Console.WriteLine(html);
}

@AlpeshChaudhari12345 Sure, you can. The code is pretty much the same as I have provided in my previous answer:

StructuredDocumentTag sdt = doc.GetChildNodes(NodeType.StructuredDocumentTag, true).Cast<StructuredDocumentTag>()
    .Where(x => x.Tag.StartsWith("Rangescop_")).FirstOrDefault();

Console.WriteLine(sdt.ToString(SaveFormat.Html));