static void AddCustomXmlPart(Document document) { #region #AddCustomXmlPart document.AppendText("This document contains custom XML parts."); // Add an empty custom XML part. ICustomXmlPart xmlItem = document.CustomXmlParts.Add(); // Populate the XML part with content. XmlElement elem = xmlItem.CustomXmlPartDocument.CreateElement("Employees"); elem.InnerText = "Stephen Edwards"; xmlItem.CustomXmlPartDocument.AppendChild(elem); // Use a string to specify the content for a custom XML part. string xmlString = @"<?xml version=""1.0"" encoding=""UTF-8""?> <Employees> <FirstName>Stephen</FirstName> <LastName>Edwards</LastName> <Address>4726 - 11th Ave. N.E.</Address> <City>Seattle</City> <Region>WA</Region> <PostalCode>98122</PostalCode> <Country>USA</Country> </Employees>"; document.CustomXmlParts.Insert(1, xmlString); // Add a custom XML part from a file. XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load("Documents\\Employees.xml"); document.CustomXmlParts.Add(xmlDoc); document.SaveDocument("Result.docx", DocumentFormat.OpenXml); System.Diagnostics.Process.Start("explorer.exe", "/select," + "Result.docx"); #endregion #AddCustomXmlPart }
static void StoreCustomXmlPart(IWorkbook workbook) { #region #StoreCustomXmlPart workbook.Worksheets[0].Cells["A1"].Value = "Custom Xml Test"; // Add an empty custom XML part. ICustomXmlPart part = workbook.CustomXmlParts.Add(); XmlElement elem = part.CustomXmlPartDocument.CreateElement("Person"); elem.InnerText = "Stephen Edwards"; part.CustomXmlPartDocument.AppendChild(elem); // Add an XML part created from string. string xmlString = @"<?xml version=""1.0"" encoding=""UTF-8""?> <whitepaper> <contact> <firstname>Roger</firstname> <lastname>Edwards</lastname> <phone>832-433-0025</phone> <address>1657 Wines Lane Houston, TX 77099</address> </contact> <date>2016-05-18</date> </whitepaper>"; workbook.CustomXmlParts.Add(xmlString); // Add an XML part loaded from a file. XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load("Documents\\fishes.xml"); workbook.CustomXmlParts.Add(xmlDoc); workbook.SaveDocument("Documents\\CustomXmlTest.xlsx"); System.IO.File.Copy("Documents\\CustomXmlTest.xlsx", "Documents\\CustomXmlTest.xlsx.zip", true); System.Diagnostics.Process.Start("Documents\\CustomXmlTest.xlsx.zip"); #endregion #StoreCustomXmlPart }