I need to create an object that has properties that are named dynamically like:
<users>
<user1name>john</user1name>
<user2name>max</user2name>
<user3name>asdf</user3name>
</users>
Is this possible?
Yes, absolutely. Just use it as an IDictionary<string, object>
to populate:
IDictionary<string, object> expando = new ExpandoObject();
expando["foo"] = "bar";
dynamic d = expando;
Console.WriteLine(d.foo); // bar
In your XML case, you'd loop over the elements, e.g.
var doc = XDocument.Load(file);
IDictionary<string, object> expando = new ExpandoObject();
foreach (var element in doc.Root.Elements())
{
expando[element.Name.LocalName] = (string) element;
}
See more on this question at Stackoverflow