コード例 #1
0
        private static string CreateCustomXmlPart(MainDocumentPart mainDocumentPart, XElement rootElement)
        {
            CustomXmlPart customXmlPart = mainDocumentPart.AddCustomXmlPart(CustomXmlPartType.CustomXml);

            customXmlPart.PutXDocument(new XDocument(rootElement));
            return(CreateCustomXmlPropertiesPart(customXmlPart));
        }
コード例 #2
0
        /// <summary>
        /// Adds the custom XML part with a custom XML properties to the main document part.
        /// </summary>
        /// <param name="mainPart">The main document part.</param>
        /// <param name="customXml">The custom XML.</param>
        /// <param name="customXmlProperties">The custom XML properties.</param>
        public static void AddCustomXmlPart(
            this MainDocumentPart mainPart, XDocument customXml, XDocument customXmlProperties)
        {
            CustomXmlPart           customXmlPart           = mainPart.AddNewPart <CustomXmlPart>();
            CustomXmlPropertiesPart customXmlPropertiesPart = customXmlPart.AddNewPart <CustomXmlPropertiesPart>();

            customXmlPropertiesPart.PutXDocument(customXmlProperties);
            customXmlPart.PutXDocument(customXml);
        }
コード例 #3
0
        public void CanUpdateCustomXmlAndMainDocumentPart()
        {
            // Define the initial and updated values of our custom XML element and
            // the data-bound w:sdt element.
            const string initialValue = "VALUE1";
            const string updatedValue = "value2";

            // Create the root element of the custom XML part with the initial value.
            var customXmlRoot =
                new XElement(Ns + "Root",
                             new XAttribute(XNamespace.Xmlns + NsPrefix, NsName),
                             new XElement(Ns + "Node", initialValue));

            // Create the w:sdtContent child element of our w:sdt with the initial value.
            var sdtContent =
                new XElement(W.sdtContent,
                             new XElement(W.p,
                                          new XElement(W.r,
                                                       new XElement(W.t, initialValue))));

            // Create a WordprocessingDocument with the initial values.
            using var stream = new MemoryStream();
            using (WordprocessingDocument wordDocument = WordprocessingDocument.Create(stream, Type))
            {
                InitializeWordprocessingDocument(wordDocument, customXmlRoot, sdtContent);
            }

            // Assert the WordprocessingDocument has the expected, initial values.
            using (WordprocessingDocument wordDocument = WordprocessingDocument.Open(stream, true))
            {
                AssertValuesAreAsExpected(wordDocument, initialValue);
            }

            // Update the WordprocessingDocument, using the updated value.
            using (WordprocessingDocument wordDocument = WordprocessingDocument.Open(stream, true))
            {
                MainDocumentPart mainDocumentPart = wordDocument.MainDocumentPart;

                // Change custom XML element, again using the simplifying assumption
                // that we only have a single custom XML part and a single ex:Node
                // element.
                CustomXmlPart customXmlPart = mainDocumentPart.CustomXmlParts.Single();
                XElement      root          = customXmlPart.GetXElement();
                XElement      node          = root.Elements(Ns + "Node").Single();
                node.Value = updatedValue;
                customXmlPart.PutXDocument();

                // Change the w:sdt contained in the MainDocumentPart.
                XElement document = mainDocumentPart.GetXElement();
                XElement sdt      = FindSdtWithTag("Node", document);
                sdtContent = sdt.Elements(W.sdtContent).Single();
                sdtContent.ReplaceAll(
                    new XElement(W.p,
                                 new XElement(W.r,
                                              new XElement(W.t, updatedValue))));

                mainDocumentPart.PutXDocument();
            }

            // Assert the WordprocessingDocument has the expected, updated values.
            using (WordprocessingDocument wordDocument = WordprocessingDocument.Open(stream, true))
            {
                AssertValuesAreAsExpected(wordDocument, updatedValue);
            }
        }
コード例 #4
0
        /// <summary>
        /// Adds the custom XML part to the main document part.
        /// </summary>
        /// <param name="document">The main document part.</param>
        /// <param name="customXml">The custom XML.</param>
        /// <returns>The <see cref="CustomXmlPart" />.</returns>
        public static void AddCustomXmlPart(this MainDocumentPart document, XDocument customXml)
        {
            CustomXmlPart customXmlPart = document.AddNewPart <CustomXmlPart>();

            customXmlPart.PutXDocument(customXml);
        }