Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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,

like image 948
SharePoint Newbie Avatar asked Oct 16 '25 20:10

SharePoint Newbie


1 Answers

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

      /* ... */

    }
 }
like image 114
Mark Cidade Avatar answered Oct 18 '25 12:10

Mark Cidade



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!