如何使用XML文件中的公式? [英] How to use formulas from a XML file?

查看:252
本文介绍了如何使用XML文件中的公式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我必须将业务逻辑存储到XML中,这样可以在程序编译后轻松更改公式。



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

推荐答案

XML中没有任何预定义的公式概念。因此,您可以使用适当的数据模式和表达式处理等开发自己的系统。 XML不再是用于存储数据的媒体。



基本上,您有两个主要选择:未解析的表达式和解析。



在未解析的方法中,表达式可以是任何文本内容,并且软件处理单元将按原样使用该文本,无论它是否有效。它可能看起来像

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
<Expression>
   <Operands>
      <Operands>x</Operands>
      <Operands>y</Operands>
   </Operands>
   <Function>(x + y - x/y) * (x-y) / 5</Function>
</Expression>



在解析方法中,XML可以表示表达式的结构。


In parsed approach, XML can represent the structure of expression.

<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>


在解析的方法中,XML解析器将表达式解析为变量名和常量等基元的大部分工作,并指示运算符;不是 Expression 元素是递归的。



我希望你有这个主意。



-SA


我能想到的唯一解决方案是Microsoft脚本控件库。它可以让您评估用VBScript或JScript编写的表达式。

请注意,它是一个COM库,为了使用它,您需要添加对Microsoft Script Control 1.0的引用。 />


以下是如何执行此操作:

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:
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:

// Output: 15
Console.WriteLine(CalculateFormula("x+y", 12, 3));
// Output: 100
Console.WriteLine(CalculateFormula("(x + y - x/y) * (x-y) / 5", 25, 5));



如果那不适合你,那么你将需要寻找一些现有的.NET公式评估器,解析器,引擎或解释器。您可以在CodeProject上找到其中的一些。


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.


这篇关于如何使用XML文件中的公式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆