Transform XElement to Anonymous type dynamicaly

Davor Cacic 1 Reputation point
2021-05-11T20:49:21.717+00:00

Hi,

I am trying to generate an anonymous type based on the XElement class, named e.g. element. This class contains properties that i can get with element.Elements(). This will return a List<XElement>.
I would like to create new anonymous type based on element.Elements(). I tried couple of approaches, but I always end up to situation where I need to iterate through list and add new property to anonymous type one by one, which by my poor knowledge in C# is not possible.

Is there a way to achieve this?

EDIT:
Hi Karen,

Thanks for the replay.

I was interested more in theory, if it is possible to be done. However, I understand that there is no enough information for getting clear picture of my problem. So, here it is:

I have a CIM XML files containing geographical data about electrical network. The files contain lot of different elements, like transformers, many types of switches, conductors, etc. I will give brief insight of what is the content of the file, and this is just one example of many type of devices, and many instances of a one type:

<cim:LoadBreak rdf:ID="123.987">
    <cim:ConductingEquipment.phases>ABC</cim:ConductingEquipment.phases>
    <cim:IdentifiedObject.mRID>123.987</cim:IdentifiedObject.mRID>
    <cim:Switch.normalOpen>false</cim:Switch.normalOpen>
    <cim:Switch.ratedCurrent>630</cim:Switch.ratedCurrent>
    <nsd:Equipment.PhasesCount>3</nsd:Equipment.PhasesCount>
    <nsd:Equipment.Voltage>12000</nsd:Equipment.Voltage>
    <cim:Equipment.EquipmentContainer rdf:resource="#Feeder1" />
  </cim:LoadBreak>

So, to help myself in regular work, I tried to develop query application where a can load the file, select type I want to list, then construct some query. Something like
Select all LoadBreak entities that have Equipment.Voltage = 12000.

This works fine, and result is - I am getting the list of selected entities in List<XElement>.

I wanted to make DataGridView on UI of application to show all these properties for some element type in tabular view.

Problem:
If you pass List<XElement> to the source of DataGridView, it shows on the UI all properties defined in XElement class. I want to show cim:ConductingEquipment.phases, cim:IdentifiedObject.mRID, etc. I don't have a model of every class in application, like LoadBreak, Transformer, Breaker, etc. That is because from different sets of files I have different naming standard, and the same structure. That means that I need to create some kind of the mapping between every set of files and my application, which is very time consuming.
So i tried to dynamically create anonymous type for every entity in the List<XElement>. Within this list, all entities are of the same "type", like LoadBreak showed above. It has 7 properties, but if I select different type it may have, for example, 20 or any other number.

So, at the end, my question is - is there a way to create anonymous type from XElement? So far, I was not able to do so, because I need to iterate through every element's properties, and as far as i could find, you cannot expand anonymous type's properties iteratively.
Another way is to have model class for every type of XElement, however that is very time consuming, since i have different sets with different naming standards.

Regards,
Davor

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,552 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.