Join
Sign in
Search Options
Search Everything
Search Formats
Home
Blog
Resources
Forums
About Open XML
More ...
Home
»
Forums
»
Formats
»
WordprocessingML
»
Removing the SdtBlock and leaving only the word ml
Re: Removing the SdtBlock and leaving only the word ml
Formats
Discussions about working with different Open XML Formats
Get this RSS feed
Details
1
Reply
0
Subscribers
Posted
over 2 years ago
Options
Subscribe via RSS
Share this
WordprocessingML
Removing the SdtBlock and leaving only the word ml
rated by 0 users
This post has
1 Reply |
0
Followers
syvers
55
Posted by
syvers
on
Tue, Feb 9 2010 8:26 AM
rated by 0 users
Post Points: 5
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
syvers
55
Posted by
syvers
on
Tue, Feb 9 2010 10:25 AM
rated by 0 users
Post Points: 5
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();
}
}
Page 1 of 1 (2 items)