NanoXLSX.Reader 3.1.0
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 : IPluginBaseReader
24 {
25 private Stream stream;
26
27 #region properties
31 public Workbook Workbook { get; set; }
35 public IOptions Options { get; set; }
39 public Action<Stream, Workbook, string, IOptions, int?> InlinePluginHandler { get; set; }
40 #endregion
41
42 #region constructors
46 internal MetadataCoreReader()
47 {
48 }
49
50 #endregion
51
52 #region methods
60 public void Init(Stream stream, Workbook workbook, IOptions readerOptions, Action<Stream, Workbook, string, IOptions, int?> inlinePluginHandler)
61 {
62 this.stream = stream;
63 this.Workbook = workbook;
64 this.Options = readerOptions;
65 this.InlinePluginHandler = inlinePluginHandler;
66 }
67
72 public void Execute()
73 {
74 try
75 {
76 using (stream) // Close after processing
77 {
78 Metadata metadata = Workbook.WorkbookMetadata;
79 using (XmlReader reader = XmlReader.Create(stream, XmlStreamUtils.CreateSettings()))
80 {
81 while (reader.Read())
82 {
83 if (reader.NodeType != XmlNodeType.Element)
84 {
85 continue;
86 }
87 if (XmlStreamUtils.IsElement(reader, "Category"))
88 {
89 metadata.Category = XmlStreamUtils.ReadElementText(reader);
90 }
91 else if (XmlStreamUtils.IsElement(reader, "ContentStatus"))
92 {
93 metadata.ContentStatus = XmlStreamUtils.ReadElementText(reader);
94 }
95 else if (XmlStreamUtils.IsElement(reader, "Creator"))
96 {
97 metadata.Creator = XmlStreamUtils.ReadElementText(reader);
98 }
99 else if (XmlStreamUtils.IsElement(reader, "Description"))
100 {
101 metadata.Description = XmlStreamUtils.ReadElementText(reader);
102 }
103 else if (XmlStreamUtils.IsElement(reader, "Keywords"))
104 {
105 metadata.Keywords = XmlStreamUtils.ReadElementText(reader);
106 }
107 else if (XmlStreamUtils.IsElement(reader, "Subject"))
108 {
109 metadata.Subject = XmlStreamUtils.ReadElementText(reader);
110 }
111 else if (XmlStreamUtils.IsElement(reader, "Title"))
112 {
113 metadata.Title = XmlStreamUtils.ReadElementText(reader);
114 }
115 }
116 InlinePluginHandler?.Invoke(stream, Workbook, PlugInUUID.MetadataCoreInlineReader, Options, null);
117 }
118 }
119 }
120 catch (Exception ex)
121 {
122 throw new IOException("The XML entry could not be read from the input stream. Please see the inner exception:", ex);
123 }
124
125 }
126 #endregion
127 }
128}
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).
Exceptions.IOException IOException