public static void ReplaceWithContentValue(this SdtElement Element, string?Content = null) { var content = Element.GetContent().ToArray(); var first = true; foreach (var content_element in content) { content_element.Remove(); if (Content == null || !first) { continue; } var run = content_element as Run ?? content_element.DescendantChilds <Run>().FirstOrDefault(); if (run is null) { continue; } first = false; run.Text(Content); Element.InsertAfterSelf(content_element); } Element.Remove(); }
public static OpenXmlElement ReplaceWithContent(this SdtElement element) { var sdt_content = element.ChildElements.First(e => e.LocalName.StartsWith("sdt") && !e.LocalName.EndsWith("Pr")); var content = sdt_content.FirstChild ?? throw new InvalidOperationException("Не найдено содержимое шаблонного элемента"); content.Remove(); element.InsertAfterSelf(content); element.Remove(); return(content); }