Beispiel #1
0
        /// <summary>
        /// Set a node's value given its hierarchical path.
        /// </summary>

        public DataNode SetHierarchy(string path, object obj)
        {
            DataNode node = this;

            if (!string.IsNullOrEmpty(path))
            {
                if (path.IndexOf('\\') == -1 && path.IndexOf('/') == -1)
                {
                    if (obj == null)
                    {
                        RemoveChild(path);
                        return(null);
                    }

                    node = GetChild(path, true);
                }
                else
                {
                    path = path.Replace("\\", "/");
                    string[] names  = path.Split('/');
                    DataNode parent = null;
                    int      index  = 0;

                    while (node != null && index < names.Length)
                    {
                        bool found = false;

                        for (int i = 0; i < node.children.size; ++i)
                        {
                            if (node.children[i].name == names[index])
                            {
                                parent = node;
                                node   = node.children[i];
                                ++index;
                                found = true;
                                break;
                            }
                        }

                        if (!found)
                        {
                            if (obj == null)
                            {
                                break;
                            }
                            parent = node;
                            node   = node.AddChild(names[index]);
                            ++index;
                        }
                    }

                    if (obj == null)
                    {
                        if (parent != null)
                        {
                            parent.RemoveChild(names[index - 1]);
                        }
                        return(parent);
                    }
                }
            }

            if (obj is DataNode)
            {
                DataNode other = (obj as DataNode);
                node.value = other.value;
                node.children.Clear();
                for (int i = 0; i < other.children.size; ++i)
                {
                    node.children.Add(other.children[i].Clone());
                }
            }
            else
            {
                node.value = obj;
            }
            return(node);
        }