Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I have to store the Business logic into the XML, this would make it easy to change formulas once program is compiled.

For Example - f(x,y) = x+y or some complex operation - f(x,y) = (x + y - x/y) * (x-y) / 5
Posted
Updated 12-Mar-15 2:12am
v3
Comments
nagendrathecoder 12-Mar-15 8:07am    
So what's the problem? You can get examples for reading and writing to xml files from google.

There is no any predefined concept of formula in XML. So, you can develop you own system with appropriate data schema and processing of expressions and so on. XML is no more then a media for storing data.

Basically, you have two major choices: unparsed expressions and parsed.

In unparsed approach, the expression can be any text content, and the software processing unit will take this text as is, no matter if it is valid or not. It may look like
HTML
<Expression>
   <Operands>
      <Operands>x</Operands>
      <Operands>y</Operands>
   </Operands>
   <Function>(x + y - x/y) * (x-y) / 5</Function>
</Expression>

In parsed approach, XML can represent the structure of expression.
HTML
<Expression operator="/">
   <Operand>
      <Expression operator="*">
          <Operand>...</Operand> <!-- represent sub-expression x + y - x/y -->
          <Operand>
              <Expression operator="-"> <!-- represent sub-expression x - y below: -->
                 <Operand>x<Operand> <!-- you may need to indicate this is a named variable, or get it from content -->
                 <Operand>y<Operand>
              <Expression>
          </Operand>
      <Expression>
      <Constant>5</Constant>
   </Operand>
<Expression>

In the parsed approach, XML parser does the big part of work of the parsing of the expression into primitives like variable names and constants, with indication of operators; not that Expression element is recursive.

I hope you got the idea.

—SA
 
Share this answer
 
Comments
Mario Z 12-Mar-15 9:45am    
I honestly prefer this approach over my suggestion, it is more elegant and provides more control.
I see there is a similar suggestion mentioned on this link as well:
http://stackoverflow.com/questions/18304337/how-to-use-formulas-from-a-text-file#18306406
Sergey Alexandrovich Kryukov 12-Mar-15 11:12am    
Thank you very much.
Yes, this article shows something very close. I extensively use XML for more or less similar thing, as the media for some procedural language, in particular.
—SA
Well the only build in solution that I can think of is the Microsoft Script Control Library. It can enable you to evaluate the expressions written in VBScript or JScript.
Note that it is a COM library and in order to use it you need to add reference to "Microsoft Script Control 1.0".

Here is how you would do this:
C#
private static string CalculateFormula(string formula, int x, int y)
{
    var controler = new MSScriptControl.ScriptControl();
    controler.Language = "VBScript";
 
    return controler.Eval(
        formula.Replace("x", x.ToString())
               .Replace("y", y.ToString())).ToString();
}

And here is an example of its usage:
C#
// Output: 15
Console.WriteLine(CalculateFormula("x+y", 12, 3));
// Output: 100
Console.WriteLine(CalculateFormula("(x + y - x/y) * (x-y) / 5", 25, 5));

If that does not suite you then you will need to look for some existing .NET formula evaluator, parser, engine or interpreter. You can find quite a few of them here on CodeProject.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900