/// <summary> /// Adds the custom XML part. /// </summary> /// <param name="mainDocumentPart">The main part.</param> /// <param name="rootElementName">Name of the root element.</param> /// <returns> /// Returns CustomXmlPart /// </returns> public CustomXmlPart AddCustomXmlPart(MainDocumentPart mainDocumentPart, string rootElementName) { if (mainDocumentPart == null) { throw new ArgumentNullException("mainDocumentPart"); } if (string.IsNullOrEmpty(rootElementName)) { throw new ArgumentNullException("rootElementName"); } XName rootElementXName = XName.Get(rootElementName, this.namespaceUri); XElement rootElement = new XElement(rootElementXName); CustomXmlPart customXmlPart = mainDocumentPart.AddCustomXmlPart(CustomXmlPartType.CustomXml); CustomXmlPropertiesPart customXmlPropertiesPart = customXmlPart.AddNewPart<CustomXmlPropertiesPart>(); GenerateCustomXmlPropertiesPartContent(customXmlPropertiesPart); WriteElementToCustomXmlPart(customXmlPart, rootElement); return customXmlPart; }
private void replaceCustomXML(MainDocumentPart mainPart, string customXML) { //using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(fileName, true)) { // MainDocumentPart mainPart = wordDoc.MainDocumentPart; mainPart.DeleteParts<CustomXmlPart>(mainPart.CustomXmlParts); //Add a new customXML part and then add content CustomXmlPart customXmlPart = mainPart.AddCustomXmlPart(CustomXmlPartType.CustomXml); //copy the XML into the new part... using (StreamWriter ts = new StreamWriter(customXmlPart.GetStream())) { ts.Write(customXML); } }
private void replaceCustomXML(MainDocumentPart mainPart, string customXML) { mainPart.DeleteParts<CustomXmlPart>(mainPart.CustomXmlParts); //Add a new customXML part and then add content CustomXmlPart customXmlPart = mainPart.AddCustomXmlPart(CustomXmlPartType.CustomXml); //copy the XML into the new part... using (StreamWriter ts = new StreamWriter(customXmlPart.GetStream())) { ts.Write(customXML); } }