NanoXLSX.Reader 3.0.0-rc.5
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;
15
17{
21 [NanoXlsxPlugIn(PlugInUUID = PlugInUUID.MetadataCoreReader)]
22 public class MetadataCoreReader : IPluginBaseReader
23 {
24 private MemoryStream stream;
25
26 #region properties
30 public Workbook Workbook { get; set; }
34 public IOptions Options { get; set; }
38 public Action<MemoryStream, Workbook, string, IOptions, int?> InlinePluginHandler { get; set; }
39 #endregion
40
41 #region constructors
45 internal MetadataCoreReader()
46 {
47 }
48
49 #endregion
50
51 #region methods
59 public void Init(MemoryStream stream, Workbook workbook, IOptions readerOptions, Action<MemoryStream, Workbook, string, IOptions, int?> inlinePluginHandler)
60 {
61 this.stream = stream;
62 this.Workbook = workbook;
63 this.Options = readerOptions;
64 this.InlinePluginHandler = inlinePluginHandler;
65 }
66
71 public void Execute()
72 {
73 try
74 {
75 using (stream) // Close after processing
76 {
77 Metadata metadata = Workbook.WorkbookMetadata;
78
79 XmlDocument xr = new XmlDocument() { XmlResolver = null };
80 using (XmlReader reader = XmlReader.Create(stream, new XmlReaderSettings() { XmlResolver = null }))
81 {
82 xr.Load(reader);
83 foreach (XmlNode node in xr.DocumentElement.ChildNodes)
84 {
85 if (node.LocalName.Equals("Category", StringComparison.OrdinalIgnoreCase))
86 {
87 metadata.Category = node.InnerText;
88 }
89 else if (node.LocalName.Equals("ContentStatus", StringComparison.OrdinalIgnoreCase))
90 {
91 metadata.ContentStatus = node.InnerText;
92 }
93 else if (node.LocalName.Equals("Creator", StringComparison.OrdinalIgnoreCase))
94 {
95 metadata.Creator = node.InnerText;
96 }
97 else if (node.LocalName.Equals("Description", StringComparison.OrdinalIgnoreCase))
98 {
99 metadata.Description = node.InnerText;
100 }
101 else if (node.LocalName.Equals("Keywords", StringComparison.OrdinalIgnoreCase))
102 {
103 metadata.Keywords = node.InnerText;
104 }
105 else if (node.LocalName.Equals("Subject", StringComparison.OrdinalIgnoreCase))
106 {
107 metadata.Subject = node.InnerText;
108 }
109 else if (node.LocalName.Equals("Title", StringComparison.OrdinalIgnoreCase))
110 {
111 metadata.Title = node.InnerText;
112 }
113 }
114 InlinePluginHandler?.Invoke(stream, Workbook, PlugInUUID.MetadataCoreInlineReader, Options, null);
115 }
116 }
117 }
118 catch (Exception ex)
119 {
120 throw new IOException("The XML entry could not be read from the input stream. Please see the inner exception:", ex);
121 }
122
123 }
124 #endregion
125 }
126}
Class representing a reader for the Core metadata file (docProps) embedded in XLSX files.
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).
void Init(MemoryStream stream, Workbook workbook, IOptions readerOptions, Action< MemoryStream, Workbook, string, IOptions, int?> inlinePluginHandler)
Initialization method (interface implementation).
Action< MemoryStream, Workbook, string, IOptions, int?> InlinePluginHandler
Reference to the ReaderPlugInHandler, to be used for post operations in the Execute method.
Exceptions.IOException IOException