BlueSky.Services.XmlDashBoardService.SetElementLocaton C# (CSharp) Method

SetElementLocaton() public method

Val is target parent location. Title is new command name.commandFile is the XAML filename.
public SetElementLocaton ( string val, string Title, string commandFile, bool forcePlace, string AboveBelowSibling ) : bool?
val string
Title string
commandFile string
forcePlace bool
AboveBelowSibling string
return bool?
        public bool? SetElementLocaton(string val, string Title, string commandFile,bool forcePlace, string AboveBelowSibling)
        {
            if(string.IsNullOrEmpty(val))
                return null;
            string[] nodes = val.Split('>');

            ////reloading a latest document. Modified by Install dialog window ///
            document.Load(FileName);

            XmlElement newelement = document.CreateElement("menu");

            XmlAttribute attrib = document.CreateAttribute("id");
            attrib.Value = Title.Replace(' ', '_');
            newelement.Attributes.Append(attrib);

            attrib = document.CreateAttribute("text");
            attrib.Value = Title;
            newelement.Attributes.Append(attrib);

            attrib = document.CreateAttribute("commandtemplate");
            attrib.Value = commandFile;
            newelement.Attributes.Append(attrib);

            attrib = document.CreateAttribute("commandoutputformat");
            attrib.Value = commandFile.Replace("xaml","xml");//same filenames(dialog and out-template) but diff extensions
            newelement.Attributes.Append(attrib);

            attrib = document.CreateAttribute("owner");
            attrib.Value = "";//Newly Insalled commands are always leaf node
            newelement.Attributes.Append(attrib);

            attrib = document.CreateAttribute("nodetype");
            attrib.Value = "Leaf";//Newly Insalled commands are always leaf node
            newelement.Attributes.Append(attrib);

            XmlNode element = document.SelectSingleNode("//menus");

            foreach (string node in nodes)//Traverse to target parent, where new command should be added
            {
                if (node == "Root")
                    continue;
                if (element == null)
                    return null;
                element = element.SelectSingleNode("./menu[@text='" + node + "']");
            }
            if (element == null)//parent not found.
                return null;

            //if parent node or new node then add as child
            if (element.HasChildNodes || (element.Attributes["id"]!=null && element.Attributes["id"].Value == "new_id"))
            {
                XmlNode temp = element.SelectSingleNode("./menu[@text='" + Title + "']");
                if (temp == null || forcePlace)
                {
                    element.AppendChild(newelement);//Add as a last leaf node.(last sibling)
                }
                else
                    return false;
            }
            else /// add as sibling below target node.(target node is leaf node here)
            {
                XmlNode temp = element.ParentNode.SelectSingleNode("./menu[@text='" + Title + "']");
                if (temp == null || forcePlace)
                {
                    //if (AboveBelowSibling.Trim().Equals("Below"))
                        
                    if (AboveBelowSibling.Trim().Equals("Above"))
                        element.ParentNode.InsertBefore(newelement, element);
                    else if (AboveBelowSibling.Trim().Equals("Below"))
                        element.ParentNode.InsertAfter(newelement, element);//06Feb2013
                    else
                        element.AppendChild(newelement); // 'else' is not needed here. No harm keeping it. this can work for parent
                    // those are not owner="BSky" and not having id="new_id" and do not have any children. (if you try to overwrite
                    // "Data File" command )

                }
                else
                    return false;
            }
            //23Apr2015 document.Save(@"./Config/menu.xml");
            document.Save(string.Format(@"{0}Menu.xml", BSkyAppData.BSkyDataDirConfigFwdSlash));//23Apr2015
            return true;
        }