0

I have been doing a lot of research on my problem and can't seem to come up with a good solution.

the problem I have a list containing the following

a
a/b
a/b/c
a/b/c/d
a/b/f
a/1
a/2
t
t/1

I need to parse that into a class that can handle parent child hierarchy. Knowing that any level can have unlimited amount of children.

Any help would be appriciated.

Vince
  • 165
  • 1
  • 14

2 Answers2

2

The format of your input is almost identical to the node path strings in Sql Server hierarchies. The following StackOverflow question about parsing those will probably be very close to, if not exactly what you want.

Converting flattened hierarchical data from SQL Server into a structured JSON object with C#/Linq

Community
  • 1
  • 1
  • did not find that one in my search as i was looking for c# and asp.net tags only. That will work like a charm. thank you very much. – Vince Dec 07 '11 at 20:47
  • been pounding my head against this for the past couple hours, this sample works great only if the data comes back with actual IDs the data i have doesn't – Vince Dec 08 '11 at 01:09
1

It is quite simple, just use String.Split:

String source = "a;a/b;a/b/c;a/b/c/d";
String[] rows = source.Split(';');
foreach(String row in rows)
    Console.WriteLine(row);

The hard part is to arrange it in a tree.

Casperah
  • 4,504
  • 1
  • 19
  • 13
  • I need a parent child relationship from the List that is generated after i split the values out "a/b/c/d" this values repeat for every new path. So assume path 'top' has 2 children 'child1', child2', this is what the list would look like, "top;top/child1;top/child2" once you parse it from the comma delimited you would get 3 item in the list "top", "top/child1", "top/child2", i need to parse that into a class that can handle parent child hierarchy. Knowing that any level can have unlimited amount of children. – Vince Dec 07 '11 at 20:29