I would create a class that represents a step by reading the XML file in it using LINQ-to-XML, and then creating a step dictionary for easy searching:
var doc = XDocument.Load("xmlfile1.xml"); var steps = from step in doc.Root.Elements("step") select new Step { Id = (int)step.Element("id"), BackId = (int)step.Element("backid"), Question = (string)step.Element("question"), YesId = (int)step.Element("yesid"), NoId = (int)step.Element("noid"), }; var dict = steps.ToDictionary(step => step.Id); var currentStep = dict[3]; while (true) { switch (Ask(currentStep.Question)) { case Yes: currentStep = dict[currentStep.YesId]; break; case No: currentStep = dict[currentStep.NoId]; break; case Back: currentStep = dict[currentStep.BackId]; break; default: return; } }
(Assuming the file contains several <step> elements, under a common root.)
source share