根据xml数据更改运算符

时间:2015-09-11 10:44:59

标签: xml c#-4.0 dictionary operators

我想在if循环中构建一个动态的操作符更改器。我从XML中获取运算符大于和小于运算符并将其存储在字典中。

private static IDictionary<string, Rule> machine = new Dictionary<string, Rule>();

这个词典存储一个键以及一个3个值的类说(machineID(string),InnerText(int)和operator(greaterthan或lessthan))

在另一个类中,我试图检索InnerText并将其与保存的Opeartor进行比较,我遇到一个问题,甚至操作员也会为每个xml文件进行更改。所以我想根据下面if条件中的字典值动态更改运算符。

if (actualrange > newrange)
   machineHealth = false;
else
   machineHealth = true;

XML:

<condition type="healthy" operator="greaterthan">100</condition>
<condition type="healthy" operator="lessthan">30</condition>

我想在上面的if条件中更改运算符(&gt;或&lt;)符号,具体取决于每次xml的值。 如何做到这一点。

1 个答案:

答案 0 :(得分:0)

试试这个

 string input = "<condition type=\"healthy\" operator=\"greaterthan\">100</condition>";

            XElement condition = XElement.Parse(input);

            XAttribute _type = condition.Attribute("type");
            _type.Value = "unhealthy";

            XAttribute _operator = condition.Attribute("operator");
            _operator.Value = "lessthan";

            condition.Value = "200";​
相关问题