/// <summary> /// Initializes an <see cref="OpenXmlVisitor"/> by reading document parts into memory. /// </summary> /// <param name="result"> /// The file to which changes can be saved. /// </param> /// <exception cref="ArgumentNullException"/> public OpenXmlVisitor([NotNull] DocxFilePath result) { if (result is null) { throw new ArgumentNullException(nameof(result)); } ContentTypes = result.ReadAsXml(ContentTypesInfo.Path) ?? throw new FileNotFoundException(ContentTypesInfo.Path); Document = result.ReadAsXml() ?? throw new FileNotFoundException("word/document.xml"); DocumentRelations = result.ReadAsXml(DocumentRelsInfo.Path) ?? throw new FileNotFoundException(DocumentRelsInfo.Path); Footnotes = result.ReadAsXml("word/footnotes.xml") ?? new XElement(W + "footnotes"); FootnoteRelations = result.ReadAsXml("word/_rels/footnotes.xml.rels") ?? new XElement(P + "Relationships"); Styles = result.ReadAsXml("word/styles.xml") ?? throw new FileNotFoundException("word/styles.xml"); Numbering = result.ReadAsXml("word/numbering.xml"); Theme1 = result.ReadAsXml("word/theme/theme1.xml"); if (Numbering is null) { Numbering = new XElement(W + "numbering"); DocumentRelations = new XElement( DocumentRelations.Name, DocumentRelations.Attributes(), DocumentRelations.Elements() .Where(x => !x.Attribute("Tartget")?.Value.Equals("numbering.xml", StringComparison.OrdinalIgnoreCase) ?? true), new XElement( P + "Relationship", new XAttribute("Id", $"rId{NextDocumentRelationId}"), new XAttribute("Type", "http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering"), new XAttribute("Target", "numbering.xml"))); ContentTypes = new XElement( ContentTypes.Name, ContentTypes.Attributes(), ContentTypes.Elements() .Where(x => !x.Attribute("PartName")?.Value.Equals("/word/numbering.xml", StringComparison.OrdinalIgnoreCase) ?? true), new XElement(T + "Override", new XAttribute("PartName", "/word/numbering.xml"), new XAttribute("ContentType", "application/vnd.openxmlformats-officedocument.wordprocessingml.numbering+xml"))); } Charts = result.ReadAsXml(DocumentRelsInfo.Path) .Elements() .Select(x => x.Attribute("Target")?.Value) .Where(x => x?.StartsWith("charts/") ?? false) .Select(x => new ChartInformation(x, result.ReadAsXml($"word/{x}"))) .ToImmutableList(); }