Beispiel #1
0
        public static List <XmlNode> GetNodes(XmlNode root,
                                              string strCfgItemPath)
        {
            Debug.Assert(root != null, "GetNodes()调用错误,root参数值不能为null。");
            Debug.Assert(strCfgItemPath != null && strCfgItemPath != "", "GetNodes()调用错误,strCfgItemPath不能为null。");


            List <XmlNode> nodes = new List <XmlNode>();

            //把strpath用'/'分开
            string[] paths = strCfgItemPath.Split(new char[] { '/' });
            if (paths.Length == 0)
            {
                return(nodes);
            }

            int i = 0;

            if (paths[0] == "")
            {
                i = 1;
            }
            XmlNode nodeCurrent = root;

            for (; i < paths.Length; i++)
            {
                string strName = paths[i];

                bool bFound = false;
                foreach (XmlNode child in nodeCurrent.ChildNodes)
                {
                    if (child.NodeType != XmlNodeType.Element)
                    {
                        continue;
                    }

                    bool bThisFound = false;

                    if (String.Compare(child.Name, "database", true) == 0)
                    {
                        string strAllCaption = DatabaseUtil.GetAllCaption(child);
                        if (StringUtil.IsInList(strName, strAllCaption) == true)
                        {
                            bFound     = true;
                            bThisFound = true;
                        }
                        else
                        {
                            bThisFound = false;
                        }
                    }
                    else
                    {
                        string strChildName = DomUtil.GetAttr(child, "name");
                        if (String.Compare(strName, strChildName, true) == 0)
                        {
                            bFound     = true;
                            bThisFound = true;
                        }
                        else
                        {
                            bThisFound = false;
                        }
                    }

                    if (bThisFound == true)
                    {
                        if (i == paths.Length - 1)
                        {
                            nodes.Add(child);
                        }
                        else
                        {
                            nodeCurrent = child;
                            break;
                        }
                    }
                }

                // 本级未找到,跳出循环
                if (bFound == false)
                {
                    break;
                }
            }

            return(nodes);
        }