NanoXLSX.Reader 3.0.0-rc.5
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 IOException = NanoXLSX.Exceptions.IOException;
17
19{
20
24 [NanoXlsxPlugIn(PlugInUUID = PlugInUUID.RelationshipReader)]
25 public partial class RelationshipReader : IPluginBaseReader
26 {
27 private MemoryStream stream;
28
29 #region properties
30
34 public Workbook Workbook { get; set; }
38 public IOptions Options { get; set; }
42 public Action<MemoryStream, Workbook, string, IOptions, int?> InlinePluginHandler { get; set; }
43
44 #endregion
45
46 #region constructor
51 {
52 }
53 #endregion
54
55 #region functions
63 public void Init(MemoryStream stream, Workbook workbook, IOptions readerOptions, Action<MemoryStream, Workbook, string, IOptions, int?> inlinePluginHandler)
64 {
65 this.stream = stream;
66 this.Workbook = workbook;
67 this.Options = readerOptions;
68 this.InlinePluginHandler = inlinePluginHandler;
69 }
70
75 public void Execute()
76 {
77 if (stream == null) return;
78 try
79 {
80 XmlDocument xr;
81 using (stream) // Close after processing
82 {
83 xr = new XmlDocument
84 {
85 XmlResolver = null
86 };
87 using (XmlReader reader = XmlReader.Create(stream, new XmlReaderSettings() { XmlResolver = null }))
88 {
89 xr.Load(reader);
90 XmlNodeList relationships = xr.GetElementsByTagName("Relationship");
91 foreach (XmlNode relationship in relationships)
92 {
93 string id = ReaderUtils.GetAttribute(relationship, "Id");
94 string type = ReaderUtils.GetAttribute(relationship, "Type");
95 string target = ReaderUtils.GetAttribute(relationship, "Target");
96 if (ParserUtils.StartsWith(target, "/"))
97 {
98 target = target.TrimStart('/');
99 }
100 if (ParserUtils.NotStartsWith(target, "xl/"))
101 {
102 target = "xl/" + target;
103 }
104 Relationship rel = new Relationship
105 {
106 RID = id,
107 Type = type,
108 Target = target,
109 };
110 Workbook.AuxiliaryData.SetData(PlugInUUID.RelationshipReader, PlugInUUID.RelationshipEntity, id, rel);
111 }
112 InlinePluginHandler?.Invoke(stream, Workbook, PlugInUUID.RelationshipInlineReader, Options, null);
113 }
114 }
115 }
116 catch (Exception ex)
117 {
118 throw new IOException("The XML entry could not be read from the input stream. Please see the inner exception:", ex);
119 }
120 }
121 #endregion
122 }
123}
Static class with common util methods, used during reading XLSX files.
static string GetAttribute(XmlNode node, string targetName, string fallbackValue=null)
Gets the XML attribute of the passed XML node by its name.
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).
void Init(MemoryStream stream, Workbook workbook, IOptions readerOptions, Action< MemoryStream, Workbook, string, IOptions, int?> inlinePluginHandler)
Initialization method (interface implementation).
Workbook Workbook
Workbook reference where read data is stored (should not be null).
Action< MemoryStream, Workbook, string, IOptions, int?> InlinePluginHandler
Reference to the ReaderPlugInHandler, to be used for post operations in the Execute method.
Class to represent a workbook relation.
Exceptions.IOException IOException