File tree Expand file tree Collapse file tree 2 files changed +44
-0
lines changed
Components/XML/BExIS.Xml.Helpers Expand file tree Collapse file tree 2 files changed +44
-0
lines changed Original file line number Diff line number Diff line change 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" />
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments