c# advanced xml like string split - Help needed

Ok i am really struggling with this, if any one can help it'd be appreciated (lanuguage c#):

Example string:
[[[['1'],'2'],'3'],[[[[[[['0015,15','bob'],'99'],'98'],'97','96'],'95'],[[[['Ra','sa','pa'],'01-01-2001','bob  ',''],'']],'0045',''],'1',[['300','80','Ends, Here','0015',''],['300','84','Start','0015','']],'','0045','N','0045','']]

I need a function to split the above string into some sort of indexable array/tree using:
[] as start/stop node delimiters
, as a field delimiter within the node

The string is dynamic and could have multiple values in each child and have different amounts of childs/subchildren.  I see this as being similar to parsing xml/creating a tree but i may be wrong....

Example usage (using above string):
if i were to reference node 1,3 i would return a '1'  ([[[['1']..]..]..]
if i were to reference node 1,2,4 i would return  '97','96'[ [,,[,,[,,]]] [[[[[[[..]..]..]'97','96']..]..]..] ..]

So basically i need something that will dynamically create the tree structure that i can ref through co-ordinates.

If your wondering - this is a webserver response so i have no control over what is returned, this is needed to allow me to identify a set of values for the applicable page section.

Any help appreciated - i am especially after code that i can copy paste to get a solution
matthew greenAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
Did you find a solution?  This site is pretty much about helping people help themselves, which I am more than happy to do.  How much expertise do you have in this area, and what kind of help can I provide?
matthew greenAuthor Commented:
Hi Bob,

Thanks for the reply, no i'm still stuck - i'm going to be attacking this again today  - i'm thinking some sort of recursion which doesn't cause me any issues,  i just don't understand how store it as something that i can read and pull a set value from, once i have the main recursion sorted and understand what to store it as i should be good to go - the key thing that is causing me the issues is the dynamism required to make this reusable and suitable for purpose.  I know it is possible as the scripts on the page have to do the same thing - so this will be the 2nd route should i fail - trying to get access to the scripts that run to read this.  The other thing that is bugging me is this must be a standard type of response from a webserver, but i cant seem to find any ref to it, any assistance appreciated.

matthew greenAuthor Commented:
ok so think i have a solution - if i can work out 1 thing:
-is it possible to split either on 2 unique charecters (i.e '[' & ']') to form an array
if not is it possible to do the split on 1 then split out an array again based on the 2nd value?
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

matthew greenAuthor Commented:
p.s. this is in framework 4
matthew greenAuthor Commented:
relativelty simple i think  - seems a good nights sleep (my overnight run and reindex!!!)  has helped - think i have what i need:
MyTempString1 = MyResponse.Substring(Start, Len);
string[] separators = { "[", "]"};
arr1 = MyTempString1.Split(separators, StringSplitOptions.None );
matthew greenAuthor Commented:
ok so the above wasn't ideal i have used:

var MyRet = new List<string>();
string[] separators = { "[", "]" };
IEnumerable<string> Mystrings = SplitAndKeep(MyTempString1, separators);

public static IEnumerable<string> MySplit(string s, params string[] delims)
            var rows = new List<string>() { s };
            foreach (string delim in delims)
                for (int i = 0; i < rows.Count; i++)
                    int index = rows[i].IndexOf(delim);
                    if (index > -1
                        && rows[i].Length > index + 1)
                        string leftPart = rows[i].Substring(0, index + delim.Length);
                        string rightPart = rows[i].Substring(index + delim.Length);
                        rows[i] = leftPart;
                        rows.Insert(i + 1, rightPart);
            return rows;

Open in new window

Now i need to work out how i can parse this into nodes that i can index (i.e. first [ and last ] are node one which contains all values and so on - so still not there yet
matthew greenAuthor Commented:
gave up - then had a brain wave when not thinking about it which seems to work - got to test across a load of different pages and variations of pages, but looks promising (kept ienumeable return for now):

Start = MyResponse.IndexOf("dom.dcx['root']") + 16;
                End = MyResponse.IndexOf(";\n//", Start);
                Len = End - Start;
                MyTempString1 = MyResponse.Substring(Start, Len);
                var MyRet = new List<string>();
                MyTempString1 = MyTempString1.Replace("],[", "|").Replace(" ",""); //this defines a new value in same section
                MyTempString1 = MyTempString1.Replace("['", "<'");
                MyTempString1 = MyTempString1.Replace("[", "");
                MyTempString1 = MyTempString1.Replace("]", ">");
                MyTempString1 = MyTempString1.Replace(">,'", "><'");
                MyTempString1 = MyTempString1.Replace("',<", "'><'");
                MyTempString1 = MyTempString1.Replace("|<'", "><'");
                MyTempString1 = MyTempString1.Replace(">>'", ">'");
                MyTempString1 = MyTempString1.Replace(">><'", "><'");
                string[] separators = { ">"};
                arr1 = MyTempString1.Split(new char[] { '>' });
                IEnumerable<string> Mystrings = MySplit(MyTempString1, separators);
Guy Hengel [angelIII / a3]Billing EngineerCommented:
trying to trow in some ideas...

what about replacing each [ by <x> and each ] by a </x>  as a starter.
before that, we have to consider what the , shall give us, as well as the ' ' around the values...
the main issue is (like withing XML) that the , could be part of the data itself.

analysing, the string  ','  is a separator of data, for sure. means it would need to be replaced by </x><x>
this means that   ['   and ']  need to be replaced first by <x> and </x>
then, we might have ',  and ,'   depending on the place, but it does not matter from what I see.

so, I would suggest to do this, take your string, and do the following:

MyTempString1 = ..... ;
MyTempString1 = MyTempString1.Replace("['", "<x>");
MyTempString1 = MyTempString1.Replace("']", "</x>");
MyTempString1 = MyTempString1.Replace("[", "<x>");
MyTempString1 = MyTempString1.Replace("]", "<x>");

MyTempString1 = MyTempString1.Replace("','", "</x><x>");
MyTempString1 = MyTempString1.Replace("',", "</x><x>");
MyTempString1 = MyTempString1.Replace(",'", "</x><x>");
MyTempString1 = MyTempString1.Replace(",", "</x><x>");

maybe you need other things to do, the idea is to get a xml fragment (with x as node name on each level), which you should be able to "scan" with a simple recursive technique.
Bob LearnedCommented:
My suggestion is to use a StringBuilder class, then you don't have to create a new string every time.
Bob LearnedCommented:
If you need to split on a complex set of characters, you can use the Regex.Split method:

Regex.Split Method (String, String)

 string input = "plum--pear";
      string pattern = "-";            // Split on hyphens 

      string[] substrings = Regex.Split(input, pattern);
      foreach (string match in substrings)
         Console.WriteLine("'{0}'", match);

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
matthew greenAuthor Commented:
Thanks all will be back on this on the, will test my method, if no good will most likely convert to xml then parse (initial plan pretty conversion), never actually used String builder (I'm from vb6 school) will have a look
Bob LearnedCommented:
In .NET, strings are immutable, which means that they can't be modified.  What looks like a modification, is actually a copy into a new memory location.  With long strings, you can run into a lot of memory usage, if you don't think about the StringBuilder class.  The short-coming of the StringBuilder is that is doesn't represent the characters as a string, so you have to use the ToString at the end of all operations to get a string representation.
matthew greenAuthor Commented:
Hi Bob - string is held within module - once finished with module all refs are cleaned - as such minimal usage from a small module, so not expecting any memory issues.   Thanks for the heads up though.

As it stands I haven't had a chance to test my method yet, I will be continuing testing later on this week.

matthew greenAuthor Commented:
Morning all,

I have tried my method which seems to be working very well on pages tried so far, i also tried converting it to xml - which also works.

Many thanks for your time and sounding board esk assistance
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.