/// <summary> /// Traverses the node. /// </summary> /// <param name="name">The name.</param> /// <param name="parts">The parts.</param> /// <param name="parent">The parent.</param> /// <returns></returns> protected AjaxFieldNode TraverseNode(string name, string[] parts, AjaxFieldNode parent = null) { //get the node name string nodeName = parts[0]; //where are we looking for this node? IList <AjaxFieldNode> Nodes = parent == null ? Children : parent.Children; //get the node if we can by name AjaxFieldNode n = Nodes.SingleOrDefault(d => d.Name == nodeName); //if it doesnt exist then create it and add it to the node list if (n == null) { n = new AjaxFieldNode(nodeName); Nodes.Add(n); } //if this was the node we are looking for then return it if (nodeName == name) { return(n); } //otherwise keep traversing nodes return(TraverseNode(name, parts.Skip(1).ToArray(), n)); }
/// <summary> /// Builds the tree from ajax array key value pairs. /// An example would be something like Key:"[Object1].[Object2].[Object3].FieldName" - Value:"FieldValue" /// </summary> /// <param name="pairs">The pairs.</param> /// <returns></returns> public static AjaxFieldTree BuildTreeFromAjaxArray(List <KeyValuePair <string, string> > pairs) { //new tree AjaxFieldTree tree = new AjaxFieldTree(); //itearte over each pair and add to tree foreach (KeyValuePair <string, string> pair in pairs.Distinct()) { //split key and value string key = pair.Key; string value = pair.Value; //check whether this key has a parent node, if not then just add to fields list if (!key.Contains('.')) { tree.Fields.Add(new KeyValuePair <string, string>(key, value)); continue; } //split key into parts string [] parts = key.Split('.'); //get the field name string field = parts[parts.Length - 1]; //get the field parent name string fieldParent = parts[parts.Length - 2]; //find the node that this field should be added too, this may create a branch to find it. AjaxFieldNode node = tree.TraverseNode(fieldParent, parts); //add the field and value node.Fields.Add(new KeyValuePair <string, string>(field, value)); } return(tree); }