NanoXLSX.Reader 3.0.0-rc.2
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 © 2025
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.Plugin;
13using NanoXLSX.Registry;
14using NanoXLSX.Registry.Attributes;
15
17{
21 [NanoXlsxPlugIn(PlugInUUID = PlugInUUID.MetadataCoreReader)]
22 public class MetadataCoreReader : IPlugInReader
23 {
24 private MemoryStream stream;
25
26 #region properties
30 public Workbook Workbook { get; set; }
31 #endregion
32
33 #region constructors
37 internal MetadataCoreReader()
38 {
39 }
40
41 #endregion
42
43 #region methods
50 public void Init(MemoryStream stream, Workbook workbook, IOptions readerOptions)
51 {
52 this.stream = stream;
53 this.Workbook = workbook;
54 }
55
60 public void Execute()
61 {
62 try
63 {
64 using (stream) // Close after processing
65 {
66 Metadata metadata = Workbook.WorkbookMetadata;
67
68 XmlDocument xr = new XmlDocument() { XmlResolver = null };
69 using (XmlReader reader = XmlReader.Create(stream, new XmlReaderSettings() { XmlResolver = null }))
70 {
71 xr.Load(reader);
72 foreach (XmlNode node in xr.DocumentElement.ChildNodes)
73 {
74 if (node.LocalName.Equals("Category", StringComparison.OrdinalIgnoreCase))
75 {
76 metadata.Category = node.InnerText;
77 }
78 else if (node.LocalName.Equals("ContentStatus", StringComparison.OrdinalIgnoreCase))
79 {
80 metadata.ContentStatus = node.InnerText;
81 }
82 else if (node.LocalName.Equals("Creator", StringComparison.OrdinalIgnoreCase))
83 {
84 metadata.Creator = node.InnerText;
85 }
86 else if (node.LocalName.Equals("Description", StringComparison.OrdinalIgnoreCase))
87 {
88 metadata.Description = node.InnerText;
89 }
90 else if (node.LocalName.Equals("Keywords", StringComparison.OrdinalIgnoreCase))
91 {
92 metadata.Keywords = node.InnerText;
93 }
94 else if (node.LocalName.Equals("Subject", StringComparison.OrdinalIgnoreCase))
95 {
96 metadata.Subject = node.InnerText;
97 }
98 else if (node.LocalName.Equals("Title", StringComparison.OrdinalIgnoreCase))
99 {
100 metadata.Title = node.InnerText;
101 }
102 }
103 RederPlugInHandler.HandleInlineQueuePlugins(ref stream, Workbook, PlugInUUID.MetadataCoreInlineReader);
104 }
105 }
106 }
107 catch (Exception ex)
108 {
109 throw new IOException("The XML entry could not be read from the input stream. Please see the inner exception:", ex);
110 }
111
112 }
113 #endregion
114 }
115}
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).
void Init(MemoryStream stream, Workbook workbook, IOptions readerOptions)
Initialization method (interface implementation).
Workbook Workbook
Workbook reference where read data is stored (should not be null).
Exceptions.IOException IOException