/// <summary> /// Binds content controls to a custom XML part created or updated from the given XML document. /// </summary> /// <param name="document">The WordprocessingDocument.</param> /// <param name="rootElement">The custom XML part's root element.</param> public static void BindContentControls(this WordprocessingDocument document, XElement rootElement) { if (document == null) { throw new ArgumentNullException("document"); } if (rootElement == null) { throw new ArgumentNullException("rootElement"); } // Get or create custom XML part. This assumes that we only have a single custom // XML part for any given namespace. var destPart = document.GetCustomXmlPart(rootElement.Name.Namespace); if (destPart == null) { destPart = document.CreateCustomXmlPart(rootElement); } else { destPart.SetRootElement(rootElement); } // Bind the content controls to the destination part's XML document. document.BindContentControls(destPart); }
/// <summary> /// Reads the json data and creates a custom XML part with the same parameters. /// /// If a custom XML part with the same namespace exists it is replaced with the new data /// and if it doesn't it is added. /// /// The XDocument must have a xmlns namespace, otherwise XmlNamespaceNotFoundException is thrown /// The namespace is used to identify the newly created CustomXmlPart or to find and replace an already existing one /// </summary> /// <param name="doc">The document that will receive the custom XML part</param> /// <param name="customPart">The XML document that will get added as a custom XML part. It must have a xmlns namespace</param> /// <returns>The replaced or newly created CustomXmlPart</returns> public static CustomXmlPart AddOrReplaceCustomXmlPart(this WordprocessingDocument doc, XDocument xmlData) { var xmlNamespace = xmlData.GetXmlNamespace(); if (string.IsNullOrWhiteSpace(xmlNamespace)) { throw new XmlNamespaceNotFoundException("Xml namespace not provided in the XDocument"); } //Try to get the custom xml part and if nothing is found add it as a custom xml part var ourPart = doc.GetCustomXmlPart(xmlNamespace) ?? doc.MainDocumentPart.AddCustomXmlPart(CustomXmlPartType.CustomXml); ourPart.FeedData(xmlData); return(ourPart); }