I am creating a Word Template with a Custom XML part linked to content controls in my template. I then replace the Custom XML Part in my code using this code:
protected void ReplaceCustomXML(string fileName, 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 the content. CustomXmlPart customXmlPart = mainPart.AddCustomXmlPart(CustomXmlPartType.CustomXml);//(<CustomXmlPart>();// AddNewPart<CustomXmlPart>(); //Copy the XML into the new part. using (StreamWriter ts = new StreamWriter(customXmlPart.GetStream())) ts.Write(customXML); } }
My original custom XML is this:
<Person><Name>John Smith</Name><Age>15</Age><Phone>555-5555</Phone></Person>
My new custom XML is this:
<Person><Name>Jane Doe</Name><Age>25</Age></Person>
Is there a way to not show the Phone number if it doesn't exist in the new XML? Right now it shows the Phone number that is set in the Word Template since it can't find a new phone number.