public void TestSave() { using (var docxStream = AssemblyResourceHelper.GetResourceStream(this, "DocxPackageHeaderFooterTest.docx")) { var memoryStream = new MemoryStream(); memoryStream.Seek(0, SeekOrigin.Begin); docxStream.Seek(0, SeekOrigin.Begin); docxStream.CopyTo(memoryStream); var package = new DocxPackage(memoryStream); package.Load(); foreach (var part in package.Parts) { XElement sdt = part.PartXml.Descendants(WordMl.SdtName).First(); sdt.Remove(); } package.Save(); var expectedDocumentStream = AssemblyResourceHelper.GetResourceStream(this, "DocxPackageHeaderFooterTest_saved.docx"); DocxPackage expectedPackagePart = new DocxPackage(expectedDocumentStream).Load(); DocxPackage actualPackagePart = new DocxPackage(memoryStream).Load(); foreach (var expected in expectedPackagePart.Parts) { var actual = actualPackagePart.Parts.Single(x => x.PartUri == expected.PartUri); Assert.AreEqual(expected.PartXml.Root.ToString().Trim(), actual.PartXml.Root.ToString().Trim()); } } }
public void TestSave() { using (var docxStream = AssemblyResourceHelper.GetResourceStream(this, "DocxPackageTest.docx")) { var memoryStream = new MemoryStream(); memoryStream.Seek(0, SeekOrigin.Begin); docxStream.Seek(0, SeekOrigin.Begin); docxStream.CopyTo(memoryStream); var testPackage = new DocxPackage(memoryStream); testPackage.Load(); var documentPart = testPackage.Parts.Single(p => p.PartUri.OriginalString.Contains("document.xml")).PartXml; XElement sdt = documentPart.Descendants(WordMl.SdtName).First(); sdt.Remove(); testPackage.Save(); var expectedDocumentStream = AssemblyResourceHelper.GetResourceStream(this, "DocxPackageTest_save.docx"); DocxPackage expectedPackagePart = new DocxPackage(expectedDocumentStream).Load(); DocxPackage actualPackagePart = new DocxPackage(memoryStream).Load(); Assert.AreEqual(expectedPackagePart.Parts.Single(p => p.PartUri.OriginalString.Contains("document.xml")).PartXml.Root.ToString().Trim(), actualPackagePart.Parts.Single(p => p.PartUri.OriginalString.Contains("document.xml")).PartXml.Root.ToString().Trim()); } }