NanoXLSX.Reader 3.2.1
Loading...
Searching...
No Matches
MetadataCoreReader.cs
1/*
2 * NanoXLSX is a small .NET library to generate and read XLSX (Microsoft Excel 2007 or newer) files in an easy and native way
3 * Copyright Raphael Stoeckli © 2026
4 * This library is licensed under the MIT License.
5 * You find a copy of the license in project folder or on: http://opensource.org/licenses/MIT
6 */
7
8using System;
9using System.IO;
10using System.Xml;
11using NanoXLSX.Interfaces;
12using NanoXLSX.Interfaces.Reader;
13using NanoXLSX.Registry;
14using NanoXLSX.Registry.Attributes;
15using NanoXLSX.Utils.Xml;
16
18{
22 [NanoXlsxPlugIn(PlugInUUID = PlugInUUID.MetadataCoreReader)]
23 public class MetadataCoreReader : IDocumentReader
24 {
25 private Stream stream;
26
27 #region properties
31 public virtual string DocumentType { get { return @"http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties"; } }
32
36 public Workbook Workbook { get; set; }
40 public IOptions Options { get; set; }
44 public Action<Stream, Workbook, string, IOptions, int?> InlinePluginHandler { get; set; }
45 #endregion
46
47 #region constructors
51 internal MetadataCoreReader()
52 {
53 }
54
55 #endregion
56
57 #region methods
65 public void Init(Stream stream, Workbook workbook, IOptions readerOptions, Action<Stream, Workbook, string, IOptions, int?> inlinePluginHandler)
66 {
67 this.stream = stream;
68 this.Workbook = workbook;
69 this.Options = readerOptions;
70 this.InlinePluginHandler = inlinePluginHandler;
71 }
72
77 public void Execute()
78 {
79 try
80 {
81 using (stream) // Close after processing
82 {
83 Metadata metadata = Workbook.WorkbookMetadata;
84 using (XmlReader reader = XmlReader.Create(stream, XmlStreamUtils.CreateSettings()))
85 {
86 while (reader.Read())
87 {
88 if (reader.NodeType != XmlNodeType.Element)
89 {
90 continue;
91 }
92 if (XmlStreamUtils.IsElement(reader, "Category"))
93 {
94 metadata.Category = XmlStreamUtils.ReadElementText(reader);
95 }
96 else if (XmlStreamUtils.IsElement(reader, "ContentStatus"))
97 {
98 metadata.ContentStatus = XmlStreamUtils.ReadElementText(reader);
99 }
100 else if (XmlStreamUtils.IsElement(reader, "Creator"))
101 {
102 metadata.Creator = XmlStreamUtils.ReadElementText(reader);
103 }
104 else if (XmlStreamUtils.IsElement(reader, "Description"))
105 {
106 metadata.Description = XmlStreamUtils.ReadElementText(reader);
107 }
108 else if (XmlStreamUtils.IsElement(reader, "Keywords"))
109 {
110 metadata.Keywords = XmlStreamUtils.ReadElementText(reader);
111 }
112 else if (XmlStreamUtils.IsElement(reader, "Subject"))
113 {
114 metadata.Subject = XmlStreamUtils.ReadElementText(reader);
115 }
116 else if (XmlStreamUtils.IsElement(reader, "Title"))
117 {
118 metadata.Title = XmlStreamUtils.ReadElementText(reader);
119 }
120 }
121 InlinePluginHandler?.Invoke(stream, Workbook, PlugInUUID.MetadataCoreInlineReader, Options, null);
122 }
123 }
124 }
125 catch (Exception ex)
126 {
127 throw new IOException("The XML entry could not be read from the input stream. Please see the inner exception:", ex);
128 }
129
130 }
131 #endregion
132 }
133}
Class representing a reader for the Core metadata file (docProps) embedded in XLSX files.
Action< Stream, Workbook, string, IOptions, int?> InlinePluginHandler
Reference to the ReaderPlugInHandler, to be used for post operations in the Execute method.
void Init(Stream stream, Workbook workbook, IOptions readerOptions, Action< Stream, Workbook, string, IOptions, int?> inlinePluginHandler)
Initialization method (interface implementation).
void Execute()
Method to execute the main logic of the plug-in (interface implementation).
Workbook Workbook
Workbook reference where read data is stored (should not be null).
virtual string DocumentType
Gets the relationship type URI of the core metadata document.
Exceptions.IOException IOException