0

I am reading an xml which contains elements as follows:

<xs:element name="id" type="xs:int" minOccurs="0"></xs:element>
<xs:element name="name" type="xs:string" minOccurs="0"></xs:element>

By using ExpandoObject, we can create a dynamic object and can create properties like

dynamic obj1 = new ExpandoObject();
obj1.id = 1;
obj1.name = "Shrikey";

where id and name are typecasted to int and string respectively.

But the thing which I am interested is whether I can create the "property name" also dynamically based on the content in name attribute of element.

Hope I made my question clear.

Any suggestions on how it can be done either using ExpandoObject or any other way.

Shrikey
  • 858
  • 3
  • 11
  • 34

1 Answers1

4

You can cast the ExpandoObject to an IDictionary<string,object> representing the mappings of property names to property values, and then manipulate those mappings:

IDictionary<string,object> expando = new ExpandoObject();
expando.Add("id", 1);
Servy
  • 202,030
  • 26
  • 332
  • 449