Click here to Skip to main content
15,394,785 members
Articles / Programming Languages / C#
Posted 31 Mar 2002


74 bookmarked

XML Finite State Machine in C#

Rate me:
Please Sign up or sign in to vote.
4.59/5 (15 votes)
31 Mar 2002MIT2 min read
An article on implementing a table-driven finite state machine using XML and C#


This article demonstrates using the .NET Framework to implement a finite state machine (FSM). This project was inspired by the MSDN article Design Patterns: Solidify Your C# Application Architecture with Design Patterns. Like the article's author, I believe that using State objects is a more powerful way of implementing this particular design pattern. However, for those that belive in XP's principle of "doing the simplest thing that could possibly work", table-driven FSMs are quite simple to implement.

To make the implementation of the FSM more interesting, I chose to define the table using XML. The format is very simple and defines state and transition elements. Using the example from the MSDN article, here is the state table in XML.

<?xml version="1.0" ?>

<fsm name="Vending Machine">
        <state name="start">
            <transition input="nickel" next="five" />
            <transition input="dime" next="ten" />
            <transition input="quarter" next="start" action="dispense" />
        <state name="five">
            <transition input="nickel" next="ten" />
            <transition input="dime" next="fifteen" />
            <transition input="quarter" next="start" action="dispense" />
        <state name="ten">
            <transition input="nickel" next="fifteen" />
            <transition input="dime" next="twenty" />
            <transition input="quarter" next="start" action="dispense" />
        <state name="fifteen">
            <transition input="nickel" next="twenty" />
            <transition input="dime" next="start" action="dispense" />
            <transition input="quarter" next="start" action="dispense" />
        <state name="twenty">
            <transition input="nickel" next="start" action="dispense" />
            <transition input="dime" next="start" action="dispense" />
            <transition input="quarter" next="start" action="dispense" />

Pretty simple. The class XMLStateMachine has three properties (Action, CurrentState, and StateTable) and one method, Next. To use it, simply create an instance of XMLStateMachine, set the StateTable property to the XML file you wish to use and set the CurrentState property to the initial state.

XMLStateMachine fsm = new XMLStateMachine();<br>
fsm.StateTable = "vending.xml";<br>
fsm.CurrentState ="start";<br>

Then, call the Next method with an appropriate input argument. Next returns the new state as well as modifies the CurrentState property.

NAnt is used to build the sample project. It is based on the Apache Ant project, which is used to build many Java-based projects. If you cannot afford Visual Studio .NET, or prefer to use your own editor and the command line compilers I highly recommend NAnt.

The code presented here could be expanded in many ways. The Action property could be replaced with an event. The System.Xml.XPath.XPathNavigator class could be used instead of System.Xml.XmlTextReader. But, that would defeat the purpose of implementing "the simplest thing that could possibly work"!


This article, along with any associated source code and files, is licensed under The MIT License


About the Author

Kevin Stewart
Web Developer
United States United States
Kevin is a software development manager for a small consumer-oriented company in SoHo, NY. He is technology and platform agnostic having worked on PCs, Macintosh and various forms of Unix and Linux. His programming knowledge includes several languages, including C/C++, Java and C#. In the rare moments when his head is not buried in the latest tech book purchase from Amazon, Kevin enjoys spending time with his wife Donna and their dog Kirby.

Comments and Discussions

QuestionInner tableParser.Read() goes beyond a particular state tag Pin
jeffry copps13-Jun-16 2:05
Memberjeffry copps13-Jun-16 2:05 
QuestionFall-through Pin
Arnoldo Ellerkamp12-Jan-12 6:53
MemberArnoldo Ellerkamp12-Jan-12 6:53 
AnswerRe: Fall-through Pin
jeffry copps13-Jun-16 2:24
Memberjeffry copps13-Jun-16 2:24 
GeneralLimitations and Hierarchy Pin
dakash20-May-09 2:57
Memberdakash20-May-09 2:57 
QuestionHow to expand the action property with an event... Pin
kimfinkis20-Jun-08 6:55
Memberkimfinkis20-Jun-08 6:55 
Generalsteed,net Pin
marc.thom8-Jun-06 5:03
Membermarc.thom8-Jun-06 5:03 
GeneralCorrected link to MSDN article... Pin
Charles T II28-Mar-05 15:30
MemberCharles T II28-Mar-05 15:30 
Generalabstract DoAction() makes forgetting the action impossible Pin
Bryan White28-Sep-03 13:39
MemberBryan White28-Sep-03 13:39 
GeneralRe: abstract DoAction() makes forgetting the action impossible Pin
Kevin Stewart29-Sep-03 3:26
MemberKevin Stewart29-Sep-03 3:26 
GeneralURL Problems Pin
James T. Johnson1-Apr-02 18:10
MemberJames T. Johnson1-Apr-02 18:10 
GeneralRe: URL Problems Pin
Kevin Stewart2-Apr-02 4:45
MemberKevin Stewart2-Apr-02 4:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.