NanoXLSX.Reader 3.1.0
Loading...
Searching...
No Matches
RelationshipReader.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;
16using NanoXLSX.Utils.Xml;
17using IOException = NanoXLSX.Exceptions.IOException;
18
20{
21
25 [NanoXlsxPlugIn(PlugInUUID = PlugInUUID.RelationshipReader)]
26 public partial class RelationshipReader : IPluginBaseReader
27 {
28 private Stream stream;
29
30 #region properties
31
35 public Workbook Workbook { get; set; }
39 public IOptions Options { get; set; }
43 public Action<Stream, Workbook, string, IOptions, int?> InlinePluginHandler { get; set; }
44
45 #endregion
46
47 #region constructor
52 {
53 }
54 #endregion
55
56 #region functions
64 public void Init(Stream stream, Workbook workbook, IOptions readerOptions, Action<Stream, Workbook, string, IOptions, int?> inlinePluginHandler)
65 {
66 this.stream = stream;
67 this.Workbook = workbook;
68 this.Options = readerOptions;
69 this.InlinePluginHandler = inlinePluginHandler;
70 }
71
76 public void Execute()
77 {
78 if (stream == null) return;
79 try
80 {
81 using (stream) // Close after processing
82 {
83 using (XmlReader reader = XmlReader.Create(stream, XmlStreamUtils.CreateSettings()))
84 {
85 while (reader.Read())
86 {
87 if (!XmlStreamUtils.IsElement(reader, "Relationship"))
88 {
89 continue;
90 }
91 string id = reader.GetAttribute("Id");
92 string type = reader.GetAttribute("Type");
93 string target = reader.GetAttribute("Target");
94 if (ParserUtils.StartsWith(target, "/"))
95 {
96 target = target.TrimStart('/');
97 }
98 if (ParserUtils.NotStartsWith(target, "xl/"))
99 {
100 target = "xl/" + target;
101 }
102 Relationship rel = new Relationship
103 {
104 RID = id,
105 Type = type,
106 Target = target,
107 };
108 Workbook.AuxiliaryData.SetData(PlugInUUID.RelationshipReader, PlugInUUID.RelationshipEntity, id, rel);
109 }
110 InlinePluginHandler?.Invoke(stream, Workbook, PlugInUUID.RelationshipInlineReader, Options, null);
111 }
112 }
113 }
114 catch (Exception ex)
115 {
116 throw new IOException("The XML entry could not be read from the input stream. Please see the inner exception:", ex);
117 }
118 }
119 #endregion
120 }
121}
RelationshipReader()
Default constructor - Must be defined for instantiation of the plug-ins.
void Execute()
Method to execute the main logic of the plug-in (interface implementation).
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).
Workbook Workbook
Workbook reference where read data is stored (should not be null).
Class to represent a workbook relation.
Exceptions.IOException IOException