Skip to content

Commit 2448f3c

Browse files
committed
#2251 add convert function for xml to get a valid version of the concept (because there were mismatches beforehand is an array is expected, but a single value is returned)
1 parent dae3b58 commit 2448f3c

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

Components/XML/BExIS.Xml.Helpers/BExIS.Xml.Helpers.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
</ItemGroup>
7272
<ItemGroup>
7373
<Compile Include="DatasetStore.cs" />
74+
<Compile Include="Extensions\XmlExtensions.cs" />
7475
<Compile Include="Mapping\XmlMapperManager.cs" />
7576
<Compile Include="Mapping\XmlSchemaManager.cs" />
7677
<Compile Include="SampleStore.cs" />
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Reflection;
4+
using System.Xml;
5+
6+
namespace BExIS.Xml.Helpers.Extensions
7+
{
8+
public static class XmlExtensions
9+
{
10+
public static void TransformXmlToMatchClassTypes(XmlNode node, Type classType)
11+
{
12+
foreach (XmlNode childNode in node.ChildNodes)
13+
{
14+
string propertyName = childNode.Name;
15+
PropertyInfo propertyInfo = classType.GetProperty(propertyName, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
16+
17+
if (propertyInfo != null)
18+
{
19+
// If it's a list or array type, ensure the XML node appropriately reflects that
20+
if (propertyInfo.PropertyType.IsGenericType && propertyInfo.PropertyType.GetGenericTypeDefinition() == typeof(List<>))
21+
{
22+
Type itemType = propertyInfo.PropertyType.GetGenericArguments()[0];
23+
24+
foreach (XmlNode listItem in childNode.ChildNodes)
25+
{
26+
// Recursively transform the XML node to match the item type of the collection
27+
TransformXmlToMatchClassTypes(listItem, itemType);
28+
}
29+
}
30+
else if (childNode.HasChildNodes)
31+
{
32+
TransformXmlToMatchClassTypes(childNode, propertyInfo.PropertyType);
33+
}
34+
}
35+
else
36+
{
37+
// Handle cases where the property is not found in the class type
38+
Console.WriteLine($"Property '{propertyName}' not found in '{classType.Name}'.");
39+
}
40+
}
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)