/*************************************************************************** Copyright (c) Microsoft Corporation 2011. This code is licensed using the Microsoft Public License (Ms-PL). The text of the license can be found here: http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx ***************************************************************************/ using System; using System.Linq; using System.Xml.Linq; using DocumentFormat.OpenXml.Packaging; namespace OpenXmlPowerTools { /// /// Performs operations on document custom xml /// public class CustomXmlAccessor{ /// /// Searches for a custom Xml part with a given name /// /// Name of custom Xml part /// XDocument with customXml part loaded public static XDocument Find(WmlDocument doc, string xmlPartName) { using (OpenXmlMemoryStreamDocument streamDoc = new OpenXmlMemoryStreamDocument(doc)) { WordprocessingDocument document = streamDoc.GetWordprocessingDocument(); string partName = "/" + xmlPartName; var customXmlPart = document.MainDocumentPart.CustomXmlParts.Where( t => t.Uri.OriginalString.EndsWith(partName, System.StringComparison.OrdinalIgnoreCase) ).FirstOrDefault(); if (customXmlPart == null) throw new ArgumentException("Part name '" + xmlPartName + "' not found."); return customXmlPart.GetXDocument(); } } /// /// Replaces a previously existing customXml part by another one /// /// XDocument of part to replace inside the document package /// Name of the part. public static OpenXmlPowerToolsDocument SetDocument(WmlDocument doc, XDocument customXmlDocument, string partNameOnly) { using (OpenXmlMemoryStreamDocument streamDoc = new OpenXmlMemoryStreamDocument(doc)) { using (WordprocessingDocument document = streamDoc.GetWordprocessingDocument()) { string partName = "/" + partNameOnly; var customXmlPart = document.MainDocumentPart.CustomXmlParts.Where( t => t.Uri.OriginalString.EndsWith(partName, System.StringComparison.OrdinalIgnoreCase) ).FirstOrDefault(); if (customXmlPart == null) customXmlPart = document.MainDocumentPart.AddCustomXmlPart(CustomXmlPartType.CustomXml); customXmlPart.PutXDocument(customXmlDocument); } return streamDoc.GetModifiedDocument(); } } } }