473,466 Members | 1,462 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

how to eval a statement from an xml file, in C#?

I want to build a rules engine in a rules.xml page. I was thinking it
would look like this:

- Rules
- rule1 if (0 < iTime <= .5) { iFee = 15; }
- rule2 if (.5 < iTime <= 1) { iFee = 20; }
- yada yada

Now lets say in my c# code I loop thru the Rules node of rules.xml, and I
want to evaluate each rule, given an iTime that the user inputed -- lets
say its .7. I want rule2 to evaluate and say, "Oh, iTime of .7 fits into
this equation, so iFee = 20" ! But I'm not sure how to get a string from
an xml node to evaluate as if it was c# code. Is it simply the evaluate
function or something like that? Does what I'm thinking of doing make sense
at all ? :)
TY Jason Shohet
Nov 15 '05 #1
8 2101
" Jason Shohet" <as****@hotmail.com> wrote in message
news:ug**************@TK2MSFTNGP10.phx.gbl...
I want to build a rules engine in a rules.xml page. I was thinking it
would look like this:

- Rules
- rule1 if (0 < iTime <= .5) { iFee = 15; }
- rule2 if (.5 < iTime <= 1) { iFee = 20; }
- yada yada

Now lets say in my c# code I loop thru the Rules node of rules.xml, and I
want to evaluate each rule, given an iTime that the user inputed -- lets
say its .7. I want rule2 to evaluate and say, "Oh, iTime of .7 fits into this equation, so iFee = 20" ! But I'm not sure how to get a string from an xml node to evaluate as if it was c# code. Is it simply the evaluate
function or something like that? Does what I'm thinking of doing make sense at all ? :)
TY Jason Shohet


Don't bother parsing through XML. Instead, create a serializable class that
implements what you need:

http://users.skynet.be/wvdd2/Persist...n_in__net.html

You could implement your algorithm's rules with a simple hashtable, where
the keys are times, and the values are fees:

foreach (decimal key in ht.Keys)
{
if (iTime < key)
iFee = ht[key];
}

Then, serialize the hasthable to a file, and bob is your uncle.

Erik
Nov 15 '05 #2
I'm not sure how seriealizing a class, and having that class query a table
helps. I'm probably not getting it ;)

My thoughts are, I want the rules in an xml file because they can be changed
easily w/o recompiling, or going into a database etc. I can just make the
change to the rule, in the xml node. I think there's an eval statement in
C#, and if so, In the c# codebehind, I could retrieve the rules from the
xml page. I could perhaps just loop thru those retrieved rules and do:
eval (strRule) where strRule has the retrieved rule from the xml file --
like I wrote below.

- Rules
- rule1 if (0 < iTime <= .5) { iFee = 15; }
- rule2 if (.5 < iTime <= 1) { iFee = 20; }
- yada yada

My thinking is this would give me flexibility. I could write new rules,
change existing ones in the xml and there's no database involved & nothing
to compile.... Am I smoking something or am I on to something?

VR Jason Shohet
Nov 15 '05 #3
Comments inline.

" Jason Shohet" <as****@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
I'm not sure how seriealizing a class, and having that class query a table
helps. I'm probably not getting it ;)
The point isn't so much serializing the class as deserializing it for
use in your program. Your rules (0 ... .5 -> 15, .5 ... 1 -> 20) are
serialized to an xml file, which you can change without recompiling, and
then deserialize into a simple class you can use.
My thoughts are, I want the rules in an xml file because they can be changed easily w/o recompiling, or going into a database etc. I can just make the
change to the rule, in the xml node. I think there's an eval statement in
C#, and if so, In the c# codebehind, I could retrieve the rules from the
xml page. I could perhaps just loop thru those retrieved rules and do:
eval (strRule) where strRule has the retrieved rule from the xml file --
like I wrote below.

- Rules
- rule1 if (0 < iTime <= .5) { iFee = 15; }
- rule2 if (.5 < iTime <= 1) { iFee = 20; }
- yada yada

My thinking is this would give me flexibility. I could write new rules,
change existing ones in the xml and there's no database involved & nothing
to compile.... Am I smoking something or am I on to something?


I'm not sure where you're getting the idea of using a database from.
All you need is an xml file that contains your serialized classes.

If you sit down and think about it, I bet you'll find that there are
perhaps only two or three different rule types that your users will want to
implement.

You could probably accomplish what you want using the System.CodeDom
namespace. But the result is going to be horribly insecure (executable text
files?), and quite possibly around five to ten times as much work as
implementing two or three different kinds of business rules.

Think of what you're trying to accomplish instead of how glamorous an
idea may sound.

Erik
Nov 15 '05 #4
ok cool, I never thought of having just xml out on the server, and
deserializing from it. Is what you're saying:

1. create the xml that represents a class in xml
2. just deserialize it for use in the codebehind when needed

That sounds interesting -- but how do I know the format that .NET needs it
in. I mean if I have a regular xml file out there, is that enough to
serialize and grab in .NET or does it need certain header info etc. I guess
i'm insecure about this because I've only serialized to a webservice, then
deserialized to do some work, serialized to send it back again &
unserialized on the page. I've never given thought to what the xml needs
to look like before its deserialized. I guess regular xml like i was
mentioning would work?

TY again
Nov 15 '05 #5
But XML serialization/deserialization is meant to represent data, not logic. So I don't quite see how it would help.
Nov 15 '05 #6
2 options come to mind

1. If your rule structure is consistent, you could change your DOM to look something like this

<rule lbound="0" lbound-inclusive="false" ubound=".5" unbound-inclusive="true" output="15"/><rule lbound=".5" lbound-inclusive="false" ubound="1" unbound-inclusive="true" output="20"/
...

your code could then iterate through the nodes and use the attribute values to compose the logic

2. If you're looking for a more general purpose solution, you could include the code expressions in your DOM (as you showed), iterate through the DOM to write out a full C# class definition (possibly using XSLT), and use either classes in the Reflection.Emit namespace (or the Microsoft.CSharp.Compiler class) to emit an assembly that implements your logic.
Nov 15 '05 #7
" Jason Shohet" <as****@hotmail.com> wrote in message
news:ev**************@TK2MSFTNGP12.phx.gbl...
ok cool, I never thought of having just xml out on the server, and
deserializing from it. Is what you're saying:

1. create the xml that represents a class in xml
2. just deserialize it for use in the codebehind when needed

That sounds interesting -- but how do I know the format that .NET needs it
in. I mean if I have a regular xml file out there, is that enough to
serialize and grab in .NET or does it need certain header info etc. I guess i'm insecure about this because I've only serialized to a webservice, then
deserialized to do some work, serialized to send it back again &
unserialized on the page. I've never given thought to what the xml needs
to look like before its deserialized. I guess regular xml like i was
mentioning would work?

TY again


You are correct - there are a number of headers and attribute info in a
serialized xml file that you probably don't want to worry about generating
from scratch.

The best way to create the xml file is to have .NET do it for you. If
xml hasn't been generated for a class yet, generate a default instantiation
of the class. This is similar to how many programs store their user
options.

The nice thing about this approach is you never actually have to worry
about a single line of XML code yourself. Your classes effortlessly persist
themselves from one instance of the application to another.

The pseudo code would look something like this:

ON LOAD:

if (xml file exists)
deserialize my rules from xml file
else
create a default instance of my rules

ON EXIT:

serialize my rules to xml file

Erik
Nov 15 '05 #8
Dan,
Thanks for your advice. I thought of the first one, but its not a general
solution enough, ie, what about rules that aren't 'bound' rules, and instead
are like, "If firstname starts with 'J' then do xxx". So I figure your
second bit of advice is the way to go perhaps. The only thing I don't know
is how to evaluate the logic, that came from the XML DOM. I can get it in
my C# codebehind page, into a string variable. But how to evaluate it? Can
I just do like in javascript:

Eval (strRule1);

If not, what does that Reflection.Emit namespace do -- same thing as that
Eval?

Rgds,
Jason Shohet
Nov 15 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Art | last post by:
Hello, I can't seem to get eval or compile to accept the print stmt, or any complicated statements. The documentation I've looked out doesn't explain why this is. ex: >>>...
6
by: Bob Rogers | last post by:
Given this class: class C(object): def set_x(self, x): self._x = x def get_x(self): return self._x x = property(get_x, set_x)
0
by: Michelle Keys | last post by:
Subject: DataBinder.Eval Error! Server Error in '/MSPOS' Application. ------------------------------------------------------------------------ -------- DataBinder.Eval:...
0
by: CES | last post by:
All, I was wondering is their a way of passing a variable into an eval() statement? I've created a custom eval() dll to use with my VB.net code. Every thing works fine until I try to pass a...
15
by: manstey | last post by:
Hi, I have a text file called a.txt: # comments I read it using this:
3
by: ray_usenet | last post by:
Why is this: eval('') is OK, but eval('{"active":"true"}') tells me I'm missing ";" ? Then why is it that if I put parentheses around the second statement:
4
by: Jon Slaughter | last post by:
I'm using eval to excute some mixed php and html code but I cannot debug it. I am essentially using filegetcontents to load up a php/html file and then inserting it into another php/html file and...
6
by: vasudevram | last post by:
Hi group, Question: Do eval() and exec not accept a function definition? (like 'def foo: pass) ? I wrote a function to generate other functions using something like eval("def foo: ....") but...
3
by: Abel Daniel | last post by:
Hi! A python interactive interpreter works by having the user type in some code, compiling and running that code, then printing the results. For printing, the results are turned into strings. ...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.