NanoXLSX.Reader 3.0.0-rc.2
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 © 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;
15using NanoXLSX.Utils;
16using IOException = NanoXLSX.Exceptions.IOException;
17
19{
20
24 [NanoXlsxPlugIn(PlugInUUID = PlugInUUID.RelationshipReader)]
25 public partial class RelationshipReader : IPlugInReader
26 {
27 private Workbook workbook;
28 private MemoryStream stream;
29
30 #region properties
31
35 public Workbook Workbook { get => workbook; set => workbook = value; }
36
37 #endregion
38
39 #region constructor
44 {
45 }
46 #endregion
47
48 #region functions
55 public void Init(MemoryStream stream, Workbook workbook, IOptions readerOptions)
56 {
57 this.stream = stream;
58 this.workbook = workbook;
59 }
60
65 public void Execute()
66 {
67 if (stream == null) return;
68 try
69 {
70 XmlDocument xr;
71 using (stream) // Close after processing
72 {
73 xr = new XmlDocument
74 {
75 XmlResolver = null
76 };
77 using (XmlReader reader = XmlReader.Create(stream, new XmlReaderSettings() { XmlResolver = null }))
78 {
79 xr.Load(reader);
80 XmlNodeList relationships = xr.GetElementsByTagName("Relationship");
81 foreach (XmlNode relationship in relationships)
82 {
83 string id = ReaderUtils.GetAttribute(relationship, "Id");
84 string type = ReaderUtils.GetAttribute(relationship, "Type");
85 string target = ReaderUtils.GetAttribute(relationship, "Target");
86 if (ParserUtils.StartsWith(target, "/"))
87 {
88 target = target.TrimStart('/');
89 }
90 if (ParserUtils.NotStartsWith(target, "xl/"))
91 {
92 target = "xl/" + target;
93 }
95 {
96 RID = id,
97 Type = type,
98 Target = target,
99 };
100 Workbook.AuxiliaryData.SetData(PlugInUUID.RelationshipReader, PlugInUUID.RelationshipEntity, id, rel);
101 }
102 RederPlugInHandler.HandleInlineQueuePlugins(ref stream, Workbook, PlugInUUID.RelationshipInlineReader);
103 }
104 }
105 }
106 catch (Exception ex)
107 {
108 throw new IOException("The XML entry could not be read from the input stream. Please see the inner exception:", ex);
109 }
110 }
111 #endregion
112 }
113}
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).
Workbook Workbook
Workbook reference where read data is stored (should not be null).
void Init(MemoryStream stream, Workbook workbook, IOptions readerOptions)
Initialization method (interface implementation).
Class to represent a workbook relation.
Exceptions.IOException IOException