public static CustomXmlPart ReplaceNamespace(this CustomXmlPart part, XNamespace newNs, string newPrefix) { if (part == null) { throw new ArgumentNullException("part"); } if (newNs == null) { throw new ArgumentNullException("newNs"); } var oldRoot = part.GetRootElement(); if (oldRoot == null) { return(null); } var oldNs = oldRoot.Name.Namespace; var newRoot = ReplaceNamespace(oldRoot, oldNs, newNs, newPrefix); part.SetRootElement(newRoot); return(part); }
/// <summary> /// Binds content controls to a custom XML part. /// </summary> /// <param name="document">The WordprocessingDocument.</param> /// <param name="part">The custom XML part.</param> public static void BindContentControls(this WordprocessingDocument document, CustomXmlPart part) { if (document == null) { throw new ArgumentNullException("document"); } if (part == null) { throw new ArgumentNullException("part"); } var customXmlRootElement = part.GetRootElement(); var storeItemId = part.CustomXmlPropertiesPart.DataStoreItem.ItemId.Value; // Bind w:sdt elements contained in main document part. var partRootElement = document.MainDocumentPart.RootElement; BindContentControls(partRootElement, customXmlRootElement, storeItemId); partRootElement.Save(); // Bind w:sdt elements contained in header parts. foreach (var headerRootElement in document.MainDocumentPart .HeaderParts.Select(p => p.RootElement)) { BindContentControls(headerRootElement, customXmlRootElement, storeItemId); headerRootElement.Save(); } // Bind w:sdt elements contained in footer parts. foreach (var footerRootElement in document.MainDocumentPart .FooterParts.Select(p => p.RootElement)) { BindContentControls(footerRootElement, customXmlRootElement, storeItemId); footerRootElement.Save(); } }