In C#, how can I build an Expression Tree by parsing XML?

.net c# expression-trees linq xml

Question

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
3
12/5/2008 7:12:18 PM

Accepted Answer

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]));
       } 

      /* ... */

    }
 }
5
12/5/2008 7:04:02 PM

Popular Answer

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



Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow