wordpress hit counter
Welcome to OpenXML Developer Sign in | Join | Help

Removing the SdtBlock and leaving only the word ml

Last post 02-09-2010, 10:25 AM by syvers. 1 replies.
Sort Posts: Previous Next
  •  02-09-2010, 8:26 AM 8135

    Removing the SdtBlock and leaving only the word ml

    Hi

    In my document I have SdtBlocks which I want to remove but retain the text etc that is contained within them.

    I dont want the content controls showing on my document output.

    I have managed this with SdtRuns and SdtCells but i cant work it out for SdtBlocks.

    What i have done is obtained the content as follows:

    SdtContentBlock blk = sdtBlock.GetFirstChild();

    string innerXml = blk.InnerXml;

    I then want to turn that string into an openxmlelement so that i can insert it into the document and remove the original block.

    Can I do this or should i be doing this a different way altogether?

    thanks

    Paul
  •  02-09-2010, 10:25 AM 8137 in reply to 8135

    Re: Removing the SdtBlock and leaving only the word ml

    I have just figured this out after all. Seems I need to get all the elements out of the SdtContent as an array and i can then insert the elements one by one and then delete the original sdtblock.

    Here is my code incase it is of use to anyone else:

    List sdtBlocks = doc.Document.Descendants().ToList();
    Guid guidTest;
    foreach (SdtBlock sdtBlock in sdtBlocks)
    {
    if (Guid.TryParse(sdtBlock.SdtProperties.GetFirstChild().Val.Value, out guidTest))
    {
    if (unityObjectData.ContainsKey(guidTest))
    {
    // Get parent element to do the insert on later in this code
    OpenXmlElement blockParent = sdtBlock.Parent;
    // Get an array of all elements inside the SdtContentBlock element of SdtBlock
    OpenXmlElement[] elements = sdtBlock.GetFirstChild().ChildElements.ToArray();
    // Variable to note insertion point
    OpenXmlElement InsertPoint;
    // Element to insert -- note we need to clone the current element or
    // we get an error message that it cannot be inserted as it is part of a tree
    OpenXmlElement InsertElement;
    // elements are inserted in reverse order as I cant seem to work
    // out how to get a reference to the new element that was entered in
    // order the insert the next element after.
    for (int i = elements.Count() - 1; i >= 0; i--)
    {
    InsertElement = elements[i].CloneNode(true);
    blockParent.InsertAfter(InsertElement, sdtBlock);
    }
    sdtBlock.Remove();
    }
    }
View as RSS news feed in XML