private static void AssertValuesAreAsExpected( WordprocessingDocument wordDocument, string expectedValue) { // Retrieve inner text of w:sdt element. MainDocumentPart mainDocumentPart = wordDocument.MainDocumentPart; XElement sdt = FindSdtWithTag("Node", mainDocumentPart.GetXElement()); string sdtInnerText = GetInnerText(sdt); // Retrieve inner text of custom XML element, making the simplifying // assumption that we only have a single custom XML part. In reality, // we would have to find the custom XML part to which our w:sdt elements // are bound among any number of custom XML parts. Further, in our // simplified example, we also assume there is a single ex:Node element. CustomXmlPart customXmlPart = mainDocumentPart.CustomXmlParts.Single(); XElement root = customXmlPart.GetXElement(); XElement node = root.Elements(Ns + "Node").Single(); string nodeInnerText = node.Value; // Assert those inner text are indeed equal. Assert.Equal(expectedValue, sdtInnerText); Assert.Equal(expectedValue, nodeInnerText); }
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); } }