This problem is best resolved with SAX. There are some SAX implementations in .NET, although I want .NET to have a built-in implementation.
The problem is that you only have two cycles, most of which you can go down two levels. It can be tedious to add extra loops. There is a better model.
If you should not use SAX, you need to create a universal engine that can handle any number of gates inside the gate.
It will be something like this
public void parseRules (State state, IEnumerable<Element> elements) { foreach (Element element in elements) { if (element.Name.Equals("strategy")) { parseNewStrategy(state, element); } else if (element.Name.Equals("gate")) { parseNewGate(state,element); } } }
I greatly simplify this. If you want to design this correctly, use the Visitor template. This is what the SAX engine would do. This will allow you to βvisitβ each element of the document and take certain predefined actions whenever you reach the element.
Something like that
If I get to the strategy, save his name and put a status field that says that I'm in the strategy. As soon as I got into the gate data warehouse, but I do not create an array of rules yet. If I hit a new shutter, then this new operation will be pushed onto the stack. If I hit a rule, create a new array of rules and associate it with the top most gates.
source share