c# - Using ExpandoObject when property names are dynamic, is this possible? -


i need create object has properties named dynamically like:

<users>   <user1name>john</user1name>   <user2name>max</user2name>   <user3name>asdf</user3name> </users> 

is possible?

yes, absolutely. use idictionary<string, object> populate:

idictionary<string, object> expando = new expandoobject(); expando["foo"] = "bar";  dynamic d = expando; console.writeline(d.foo); // bar 

in xml case, you'd loop on 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; } 

Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -