How do I Create an Expression Tree by Parsing Xml in C#?

别等时光非礼了梦想. 提交于 2019-12-07 13:20:16

问题


I am looking to create an expression tree by parsing xml using C#. The xml would be like the following:

<Expression>
<If>
  <Condition>
    <GreaterThan>
      <X>
      <Y>
    </GreaterThan>
  </Condition>
  <Expression />
<If>
<Else>
  <Expression />
</Else>
<Expression>

or another example...

<Expression>
  <Add>
    <X>
    <Expression>
      <Y>
      <Z>
    </Expression>
  </Add>
</Expression>

...any pointers on where to start would be helpful.

Kind regards,


回答1:


using System.Linq.Expressions; //in System.Core.dll

Expression BuildExpr(XmlNode xmlNode)
 { switch(xmlNode.Name)
    { case "Add":
       { return Expression.Add( BuildExpr(xmlNode.ChildNodes[0])
                               ,BuildExpr(xmlNode.ChilNodes[1]));
       } 

      /* ... */

    }
 }



回答2:


I'd start by looking at the DLR, which has a published expression tree mechanism.



来源:https://stackoverflow.com/questions/344741/how-do-i-create-an-expression-tree-by-parsing-xml-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!