NanoXLSX.Reader 3.0.0-rc.2
Loading...
Searching...
No Matches
ReaderUtils.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.Linq;
10using System.Xml;
11
12namespace NanoXLSX.Internal
13{
17 public static class ReaderUtils
18 {
26 public static string GetAttribute(XmlNode node, string targetName, string fallbackValue = null)
27 {
28 if (node.Attributes == null || node.Attributes.Count == 0)
29 {
30 return fallbackValue;
31 }
32
33 foreach (XmlAttribute attribute in node.Attributes)
34 {
35 if (attribute.Name == targetName)
36 {
37 return attribute.Value;
38 }
39 }
40
41 return fallbackValue;
42 }
43
53 public static bool GetAttributeOfChild(XmlNode node, string childNodeName, string attributeName, out string output)
54 {
55 XmlNode childNode = GetChildNode(node, childNodeName);
56 if (childNode != null)
57 {
58 output = GetAttribute(childNode, attributeName);
59 return true;
60 }
61 output = null;
62 return false;
63 }
64
71 public static XmlNode GetChildNode(XmlNode node, string name)
72 {
73 if (node != null && node.HasChildNodes)
74 {
75 return node.ChildNodes.Cast<XmlNode>().FirstOrDefault(c => c.LocalName.Equals(name, StringComparison.OrdinalIgnoreCase));
76 }
77 return null;
78 }
79
86 internal static bool IsNode(XmlNode node, string name)
87 {
88 return node.LocalName.Equals(name, StringComparison.OrdinalIgnoreCase);
89 }
90
97 internal static string DiscoverPrefix(XmlDocument document, string targetName)
98 {
99 foreach (XmlNode node in document.ChildNodes)
100 {
101 if (node.LocalName == targetName)
102 {
103 return node.Prefix;
104 }
105 }
106 return "";
107 }
108
116 internal static XmlNodeList GetElementsByTagName(XmlDocument document, string tagName, string prefix)
117 {
118 if (string.IsNullOrEmpty(prefix))
119 {
120 return document.GetElementsByTagName(tagName);
121 }
122 else
123 {
124 return document.GetElementsByTagName(prefix + ":" + tagName);
125 }
126 }
127
128 }
129}
Static class with common util methods, used during reading XLSX files.
static XmlNode GetChildNode(XmlNode node, string name)
Gets the specified child node.
static string GetAttribute(XmlNode node, string targetName, string fallbackValue=null)
Gets the XML attribute of the passed XML node by its name.
static bool GetAttributeOfChild(XmlNode node, string childNodeName, string attributeName, out string output)
Gets the XML attribute from a child node of the passed XML node by its name and the name of the child...