public void CanTransformWordprocessingDocumentWithAltChunksToFlatOpc() { using (Stream stream = CreateWordprocessingDocumentWithAltChunks()) using (WordprocessingDocument wordDocument = WordprocessingDocument.Open(stream, false)) { XDocument flatOpcDocument = wordDocument.ToFlatOpcDocument(); XElement mainDocumentPart = flatOpcDocument .Descendants(Pkg + "part") .First(p => p.Attributes(Pkg + "name").Any(a => a.Value == "/word/document.xml")); IEnumerable <XElement> altChunkParts = flatOpcDocument .Descendants(Pkg + "part") .Where(p => p.Attributes(Pkg + "name").Any(a => a.Value.Contains("afchunk"))) .ToList(); // We want our MainDocumentPart to contain XML data. Assert.Equal(WordprocessingMLContentType, (string)mainDocumentPart.Attribute(Pkg + "contentType")); Assert.NotEmpty(mainDocumentPart.Elements(Pkg + "xmlData")); // We want to see each one of our AlternativeFormatImportParts. Assert.Contains(altChunkParts, p => (string)p.Attribute(Pkg + "contentType") == XhtmlContentType); Assert.Contains(altChunkParts, p => (string)p.Attribute(Pkg + "contentType") == XmlContentType); Assert.Contains(altChunkParts, p => (string)p.Attribute(Pkg + "contentType") == WordprocessingMLContentType); // We want all of our AlternativeFormatImportParts to contain binary data, // even though two of them have a content type ending with "+xml". Assert.All(altChunkParts, p => Assert.NotEmpty(p.Elements(Pkg + "binaryData"))); } }
public void CanRoundTripWordprocessingDocumentWithAltChunks() { using (MemoryStream stream = CreateWordprocessingDocumentWithAltChunks()) using (WordprocessingDocument original = WordprocessingDocument.Open(stream, false)) { XDocument flatOpcDocument = original.ToFlatOpcDocument(); using (WordprocessingDocument roundTripped = WordprocessingDocument.FromFlatOpcDocument(flatOpcDocument)) { List <AlternativeFormatImportPart> altChunkParts = roundTripped.MainDocumentPart .GetPartsOfType <AlternativeFormatImportPart>() .ToList(); AssertXmlContentEquals( altChunkParts.First(p => p.ContentType == XhtmlContentType), XhtmlString); AssertXmlContentEquals( altChunkParts.First(p => p.ContentType == XmlContentType), XmlString); AssertBinaryContentEquals( altChunkParts.First(p => p.ContentType == WordprocessingMLContentType), WordprocessingMLBytes); } } }