I have the following code that generates Dynamic object from XML file:
C#
private static List<dynamic> GetClientObject()
{
var xDoc = XDocument.Load(new StreamReader(xmlPath + @"\client.xml"));
dynamic root = new ExpandoObject();
XmlToDynamic.Parse(root, xDoc.Elements().First());
List<dynamic> clients = new List<dynamic>();
for (int i = 0; i < root.clients.client.Count; i++)
{
clients.Add(new ExpandoObject());
clients[i].Id = root.clients.client[i].id;
clients[i].Name = root.clients.client[i].name;
List<string> list = new List<string>();
for (int j = 0; j < root.clients.client[i].emails.email.Count; j++)
{
list.Add(root.clients.client[i].emails.email[j].ToString());
}
clients[i].Email = string.Join(",", list);
}
return clients;
}
XML
<clients>
<client>
<id>SomeId</id>
<name>SomeName</name>
<emails>
<email>abc@xyz.com</email>
<email>def@xyz.com</email>
<email>ghi@xyz.com</email>
</emails>
<timezone>Mountain Standard Time</timezone>
</client>
</clients>
The code works fine but I always see the following Exception(multiple times) in the IntelliTrace:
Exception:Thrown: "'System.Dynamic.ExpandoObject' does not contain a definition for 'client'" (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException) A Microsoft.CSharp.RuntimeBinder.RuntimeBinderException was thrown: "'System.Dynamic.ExpandoObject' does not contain a definition for 'client'"
Is there anything wrong with my code?