I looked at this article to see if I could use that as a starting point, but I don't see it addressing my particular problem. C#: N For Loops
I have the following code:
int criteriaCount = rule.SearchCriteria.Criteria.Count();
string criteria = "";
for (int i = 0; i < criteriaCount; i++)
{
if (rule.SearchCriteria.Criteria[i].Criteria.Count() > 0)
{
criteria += string.Format("({0}" + System.Environment.NewLine, rule.SearchCriteria.Criteria[i].Display);
criteria += string.Format("{0})" + System.Environment.NewLine, rule.SearchCriteria.Criteria[i].Criteria[i].Display);
}
else
{
criteria += string.Format("[{0}]" + System.Environment.NewLine, rule.SearchCriteria.Criteria[i].Display);
}
}
To explain a little, you have in SearchCriteria and Array of Criteria[]. I can loop through this and grab the follow I need from each Criteria object as you can see I am doing.
I am also looking at the second level deep, so SearchCriteria.Criteria[n].Criteria[n] and I can put a for loop their and grab any nested values there as well.
The part I cannot figure out is how can I account for a variable number of nested Criteria objects? I could potentially have this:
SearchCriteria.Criteria[n].Criteria[n] ... (repeat a hundred times)... .Criteria[n]
So it can have potentially an infinite number of nested objects, and an infinite number of sibling objects (Criteria[0], Criteria[1] ... Criteria[100000] is what I mean).
Is there a way I can loop through all this? I have heard the a recursive loop may be the answer, and I vaguely understand that concept, but what I don't know is how can I get the number of children and siblings?