public void Process(Body Body, OpenXmlElement Element) { string InnerText = Element.InnerText; if (VerifyCodeToken(ref InnerText)) { Element.Remove(); Scripting.Eval(InnerText); return; } if (Scripting.CaptureElement(Body, Element)) { Element.Remove(); return; } if (string.IsNullOrEmpty(InnerText)) { return; } if (VerifyToken(ref InnerText, out TokenParameters TokenParams)) { DataType DataType = Data.GetObject(InnerText, TokenParams, out object Obj); if (DataType == DataType.None) { throw new Exception(string.Format("Could not find data for token '{0}'", InnerText)); } if ((CurrentRun = Element.GetFirstChild <RunProperties>()) == null) { SdtProperties SdtProps = Element.GetFirstChild <SdtProperties>(); if (SdtProps != null) { CurrentRun = SdtProps.GetFirstChild <RunProperties>(); } } if (Element is SdtContentBlock SdtContentBlock) { OpenXmlElement NewContent = GenerateContent(Body, DataType, Obj, TokenParams); // TODO: Non existant template token, do something? if (NewContent == null) { throw new NotImplementedException(); } if (NewContent is Run) { NewContent = new Paragraph(new ParagraphProperties(), NewContent); } if (DataType == DataType.PageBreak) { SdtContentBlock.Parent.Parent.ReplaceChild(NewContent, SdtContentBlock.Parent); } else { SdtContentBlock NewBlock = new SdtContentBlock(); NewBlock.AppendChild(NewContent); SdtContentBlock.Parent.ReplaceChild(NewBlock, SdtContentBlock); } return; } else if (Element is SdtContentRun SdtContentRun) { // if (SdtRun) seems to replace this part? throw new NotImplementedException(); } else if (Element is SdtRun SdtRun) { OpenXmlElement NewContent = GenerateContent(Body, DataType, Obj, TokenParams); // TODO: Non existant template token, do something? if (NewContent == null) { throw new NotImplementedException(); } if (NewContent is Table && ParentsAreElement <Paragraph>(SdtRun, out OpenXmlElement FoundParent)) { FoundParent.Parent.ReplaceChild(NewContent, FoundParent); return; } Element.Parent.ReplaceChild(NewContent, SdtRun); return; } } // Array is used here because element children are dynamically changed, IEnumerable may have side effects OpenXmlElement[] Elements = Element.Elements().ToArray(); for (int i = 0; i < Elements.Length; i++) { OpenXmlElement E = Elements[i]; if (E is Table T) { ProcessTable(Body, T); } else { Process(Body, E); } } }