| | 1 | | using System.Collections; |
| | 2 | | using System.Reflection; |
| | 3 | | using System.Xml.Linq; |
| | 4 | |
|
| | 5 | | namespace Kestrun.Utilities; |
| | 6 | |
|
| | 7 | | /// <summary> |
| | 8 | | /// Helpers for converting arbitrary objects into <see cref="XElement"/> instances. |
| | 9 | | /// </summary> |
| | 10 | | public static class XmlHelper |
| | 11 | | { |
| 1 | 12 | | private static readonly XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance"; |
| | 13 | |
|
| | 14 | | /// <summary> |
| | 15 | | /// Converts an object to an <see cref="XElement"/> with the specified name, handling nulls, primitives, dictionarie |
| | 16 | | /// </summary> |
| | 17 | | /// <param name="name">The name of the XML element.</param> |
| | 18 | | /// <param name="value">The object to convert to XML.</param> |
| | 19 | | /// <returns>An <see cref="XElement"/> representing the object.</returns> |
| | 20 | | public static XElement ToXml(string name, object? value) |
| | 21 | | { |
| | 22 | | // 1️⃣ null → <name xsi:nil="true"/> |
| 32 | 23 | | if (value is null) |
| | 24 | | { |
| 6 | 25 | | return new XElement(name, new XAttribute(xsi + "nil", true)); |
| | 26 | | } |
| | 27 | |
|
| | 28 | | // 2️⃣ Primitive or string → <name>42</name> |
| 26 | 29 | | if (IsSimple(value)) |
| | 30 | | { |
| 20 | 31 | | return new XElement(name, value); |
| | 32 | | } |
| | 33 | |
|
| | 34 | | // 3️⃣ IDictionary (generic or non-generic) |
| 6 | 35 | | if (value is IDictionary dict) |
| | 36 | | { |
| 1 | 37 | | return DictionaryToXml(name, dict); |
| | 38 | | } |
| | 39 | |
|
| | 40 | | // 4️⃣ IEnumerable (lists, arrays, StringValues, etc.) |
| 5 | 41 | | if (value is IEnumerable enumerable) |
| | 42 | | { |
| 1 | 43 | | return EnumerableToXml(name, enumerable); |
| | 44 | | } |
| | 45 | |
|
| | 46 | | // 5️⃣ Fallback: reflect public instance properties (skip indexers) |
| 4 | 47 | | return ObjectToXml(name, value); |
| | 48 | | } |
| | 49 | |
|
| | 50 | | /// <summary> |
| | 51 | | /// Determines whether the specified object is a simple type (primitive, string, DateTime, Guid, or decimal). |
| | 52 | | /// </summary> |
| | 53 | | /// <param name="value">The object to check.</param> |
| | 54 | | /// <returns><c>true</c> if the object is a simple type; otherwise, <c>false</c>.</returns> |
| | 55 | | private static bool IsSimple(object value) |
| | 56 | | { |
| 26 | 57 | | var type = value.GetType(); |
| 26 | 58 | | return type.IsPrimitive || value is string || value is DateTime || value is Guid || value is decimal; |
| | 59 | | } |
| | 60 | |
|
| | 61 | | /// <summary> |
| | 62 | | /// Converts a dictionary to an XML element. |
| | 63 | | /// </summary> |
| | 64 | | /// <param name="name">The name of the XML element.</param> |
| | 65 | | /// <param name="dict">The dictionary to convert.</param> |
| | 66 | | /// <returns>An <see cref="XElement"/> representing the dictionary.</returns> |
| | 67 | | private static XElement DictionaryToXml(string name, IDictionary dict) |
| | 68 | | { |
| 1 | 69 | | var elem = new XElement(name); |
| 6 | 70 | | foreach (DictionaryEntry entry in dict) |
| | 71 | | { |
| 2 | 72 | | var key = entry.Key?.ToString() ?? "Key"; |
| 2 | 73 | | elem.Add(ToXml(key, entry.Value)); |
| | 74 | | } |
| 1 | 75 | | return elem; |
| | 76 | | } |
| | 77 | | /// <summary> |
| | 78 | | /// Converts an enumerable collection to an XML element. |
| | 79 | | /// </summary> |
| | 80 | | /// <param name="name">The name of the XML element.</param> |
| | 81 | | /// <param name="enumerable">The enumerable collection to convert.</param> |
| | 82 | | /// <returns>An <see cref="XElement"/> representing the collection.</returns> |
| | 83 | | private static XElement EnumerableToXml(string name, IEnumerable enumerable) |
| | 84 | | { |
| 1 | 85 | | var elem = new XElement(name); |
| 6 | 86 | | foreach (var item in enumerable) |
| | 87 | | { |
| 2 | 88 | | elem.Add(ToXml("Item", item)); |
| | 89 | | } |
| 1 | 90 | | return elem; |
| | 91 | | } |
| | 92 | |
|
| | 93 | | /// <summary> |
| | 94 | | /// Converts an object to an XML element. |
| | 95 | | /// </summary> |
| | 96 | | /// <param name="name">The name of the XML element.</param> |
| | 97 | | /// <param name="value">The object to convert.</param> |
| | 98 | | /// <returns>An <see cref="XElement"/> representing the object.</returns> |
| | 99 | | private static XElement ObjectToXml(string name, object value) |
| | 100 | | { |
| 4 | 101 | | var objElem = new XElement(name); |
| 4 | 102 | | var type = value.GetType(); |
| 48 | 103 | | foreach (var prop in type.GetProperties(BindingFlags.Public | BindingFlags.Instance)) |
| | 104 | | { |
| 20 | 105 | | if (prop.GetIndexParameters().Length > 0) // <<—— SKIP INDEXERS |
| | 106 | | { |
| | 107 | | continue; |
| | 108 | | } |
| | 109 | |
|
| | 110 | | object? propVal; |
| | 111 | | try |
| | 112 | | { |
| 20 | 113 | | propVal = prop.GetValue(value); |
| 20 | 114 | | } |
| 0 | 115 | | catch |
| | 116 | | { |
| 0 | 117 | | continue; // skip unreadable props |
| | 118 | | } |
| | 119 | |
|
| 20 | 120 | | objElem.Add(ToXml(prop.Name, propVal)); |
| | 121 | | } |
| | 122 | |
|
| 4 | 123 | | return objElem; |
| | 124 | | } |
| | 125 | |
|
| | 126 | | /// <summary> |
| | 127 | | /// Converts an <see cref="XElement"/> into a <see cref="Hashtable"/>. |
| | 128 | | /// Nested elements become nested Hashtables; repeated elements become lists. |
| | 129 | | /// Attributes are stored as keys prefixed with "@", xsi:nil="true" becomes <c>null</c>. |
| | 130 | | /// </summary> |
| | 131 | | /// <param name="element">The XML element to convert.</param> |
| | 132 | | /// <returns>A Hashtable representation of the XML element.</returns> |
| | 133 | | public static Hashtable ToHashtable(XElement element) |
| | 134 | | { |
| 17 | 135 | | var table = new Hashtable(); |
| | 136 | |
|
| | 137 | | // Handle attributes (as @AttributeName) |
| 40 | 138 | | foreach (var attr in element.Attributes()) |
| | 139 | | { |
| 4 | 140 | | if (attr.Name.NamespaceName == xsi.NamespaceName && attr.Name.LocalName == "nil" && attr.Value == "true") |
| | 141 | | { |
| 2 | 142 | | return new Hashtable { [element.Name.LocalName] = null! }; |
| | 143 | | } |
| 2 | 144 | | table["@" + attr.Name.LocalName] = attr.Value; |
| | 145 | | } |
| | 146 | |
|
| | 147 | | // If element has no children → treat as value |
| 15 | 148 | | if (!element.HasElements) |
| | 149 | | { |
| 10 | 150 | | if (!string.IsNullOrWhiteSpace(element.Value)) |
| | 151 | | { |
| 9 | 152 | | table[element.Name.LocalName] = element.Value; |
| | 153 | | } |
| 10 | 154 | | return table; |
| | 155 | | } |
| | 156 | |
|
| | 157 | | // Otherwise recurse into children |
| 5 | 158 | | var childMap = new Hashtable(); |
| 28 | 159 | | foreach (var child in element.Elements()) |
| | 160 | | { |
| 9 | 161 | | var childKey = child.Name.LocalName; |
| 9 | 162 | | var childValue = ToHashtable(child); |
| | 163 | |
|
| 9 | 164 | | if (childMap.ContainsKey(childKey)) |
| | 165 | | { |
| | 166 | | // Already exists → convert to List (allowing null values) |
| 3 | 167 | | if (childMap[childKey] is List<object?> list) |
| | 168 | | { |
| 1 | 169 | | list.Add(childValue[childKey]); |
| | 170 | | } |
| | 171 | | else |
| | 172 | | { |
| 2 | 173 | | childMap[childKey] = new List<object?> |
| 2 | 174 | | { |
| 2 | 175 | | childMap[childKey], |
| 2 | 176 | | childValue[childKey] |
| 2 | 177 | | }; |
| | 178 | | } |
| | 179 | | } |
| | 180 | | else |
| | 181 | | { |
| 6 | 182 | | childMap[childKey] = childValue[childKey]; |
| | 183 | | } |
| | 184 | | } |
| | 185 | |
|
| 5 | 186 | | table[element.Name.LocalName] = childMap; |
| 5 | 187 | | return table; |
| 2 | 188 | | } |
| | 189 | | } |