1

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?

4

1 回答 1

1

我收集这是使用ExpandoObject. 我查看了此代码的 IntelliTrace 日志,您看到的异常条目是配对的:

  • 异常:抛出:“'System.Dynamic.ExpandoObject' 不包含'clients'的定义”(Microsoft.CSharp.RuntimeBinder.RuntimeBinderException)
  • 异常:捕获:“'System.Dynamic.ExpandoObject' 不包含 'clients' 的定义”(Microsoft.CSharp.RuntimeBinder.RuntimeBinderException)

即异常被抛出然后被捕获。如果您查看调用堆栈窗口,您将看到抛出和捕获位于 .NET Framework 中。

顺便说一句,我确实必须对您的代码进行一些小改动才能使其运行:我更改为:root.clients.client.Countroot.clients.Count循环中for

于 2013-08-21T17:42:53.987 回答