Exemplo n.º 1
0
        // 递归
        private TreeNodeEx FindNodeExt(TreeNodeEx tnParent, SelectedElement strValue)
        {
            if (tnParent == null)
            {
                return(null);
            }

            //if (tnParent.HtmlNode.InnerText == strValue) return tnParent;
            if (tnParent.HtmlNode.Name == strValue.tagName)
            {
                if (tnParent.HtmlNode.InnerText == strValue.innerText)
                {
                    return(tnParent);
                }
            }

            TreeNodeEx tnRet = null;

            foreach (var tn in tnParent.Nodes)
            {
                var treeNodeEx = tn as TreeNodeEx;

                tnRet = FindNodeExt(treeNodeEx, strValue);
                if (tnRet != null)
                {
                    break;
                }
            }
            return(tnRet);
        }
Exemplo n.º 2
0
        ///// <summary>
        ///// 生成目录树
        ///// </summary>
        //private void BuildTree()
        //{
        //    FileTree.BeginUpdate();

        //    //存放树节点的栈
        //    Stack<TreeNode> skNode = new Stack<TreeNode>();
        //    int imageIndex = 0;

        //    //添加磁盘列表
        //    List<string> drives = new List<string>();
        //    drives.Add(Directory.GetLogicalDrives().LastOrDefault());
        //    for (int i = 0; i < drives.Count; i++)
        //    {
        //        //每个节点的Text存放目录名,Name存放全路径
        //        TreeNode node = new TreeNode(drives[i], 0, 0);
        //        node.Name = drives[i];

        //        FileTree.Nodes.Add(node);
        //        skNode.Push(node);
        //    }

        //    while (skNode.Count > 0)
        //    {
        //        //弹出栈顶目录,并获取路径
        //        TreeNode curNode = skNode.Pop();
        //        string path = curNode.Name;

        //        FileInfo fInfo = new FileInfo(path);
        //        if ((fInfo.Attributes & FileAttributes.Directory) != 0)
        //        {
        //            string[] subDirs = null;
        //            string[] subFiles = null;
        //            try
        //            {
        //                //获取当前目录下的所有子目录和文件
        //                subDirs = Directory.GetDirectories(path);
        //                subFiles = Directory.GetFiles(path);
        //            }
        //            catch
        //            { }

        //            if (subDirs != null && subFiles != null)
        //            {
        //                //目录入栈
        //                for (int i = 0; i < subDirs.Length; i++)
        //                {
        //                    string dirName = Path.GetFileName(subDirs[i]);
        //                    TreeNode dirNode = new TreeNode(dirName, 1, 1);
        //                    dirNode.Name = subDirs[i];

        //                    curNode.Nodes.Add(dirNode);
        //                    skNode.Push(dirNode);
        //                }

        //                //文件无需入栈
        //                for (int i = 0; i < subFiles.Length; i++)
        //                {
        //                    string fileName = Path.GetFileName(subFiles[i]);
        //                    curNode.Nodes.Add(subFiles[i], fileName, 2);
        //                }
        //            }
        //        }
        //    }

        //    FileTree.EndUpdate();
        //}

        ///// <summary>
        ///// 生成目录树
        ///// </summary>
        //private void BuildTree2()
        //{
        //    FileTree.BeginUpdate();

        //    Stack<DomNode> stackDomNode=new Stack<DomNode>();
        //    foreach (var firstNode in this.htmlDocument.DocumentNode.ChildNodes)
        //    {
        //        DomNode domNode = new DomNode();
        //        string text = firstNode.Name;
        //        if (firstNode.HasAttributes)
        //        {
        //            foreach (var attr in firstNode.Attributes)
        //            {
        //                text += nbsp + attr.Name + "=" + attr.Value + nbsp;
        //            }
        //        }

        //        domNode.TreeNode = new TreeNode(text);
        //        domNode.HtmlNode = firstNode;
        //        FileTree.Nodes.Add(domNode.TreeNode);
        //        stackDomNode.Push(domNode);
        //        RootDomNode = domNode;
        //    }

        //    FileTree.EndUpdate();

        //    while (stackDomNode.Count>0)
        //    {
        //        DomNode curDomNode = stackDomNode.Pop();
        //        if(curDomNode.HtmlNode.ChildNodes.Count>0)
        //        {
        //            foreach (var node in curDomNode.HtmlNode.ChildNodes)
        //            {
        //                DomNode domNode = new DomNode();
        //                string text = node.Name;
        //                if (node.HasAttributes)
        //                {
        //                    foreach (var attr in node.Attributes)
        //                    {
        //                        text += nbsp + attr.Name + "=" + attr.Value + nbsp;
        //                    }
        //                }

        //                domNode.TreeNode = new TreeNode(text);
        //                domNode.HtmlNode = node;
        //                curDomNode.TreeNode.Nodes.Add(domNode.TreeNode);
        //                stackDomNode.Push(domNode);
        //            }
        //        }
        //    }

        //    FileTree.EndUpdate();
        //}

        /// <summary>
        /// 生成目录树
        /// </summary>
        private void BuildTree3()
        {
            HtmlTree.BeginUpdate();
            HtmlTree.Nodes.Clear();
            Stack <TreeNodeEx> stackDomNode = new Stack <TreeNodeEx>();

            foreach (var firstNode in this.htmlDocument.DocumentNode.ChildNodes)
            {
                string text = firstNode.Name;
                if (firstNode.HasAttributes)
                {
                    foreach (var attr in firstNode.Attributes)
                    {
                        text += nbsp + attr.Name + "=" + attr.Value + nbsp;
                    }
                }
                TreeNodeEx domNode = new TreeNodeEx(text);

                domNode.HtmlNode = firstNode;
                HtmlTree.Nodes.Add(domNode);
                stackDomNode.Push(domNode);
            }

            while (stackDomNode.Count > 0)
            {
                TreeNodeEx curDomNode = stackDomNode.Pop();
                if (curDomNode.HtmlNode.ChildNodes.Count > 0)
                {
                    foreach (var node in curDomNode.HtmlNode.ChildNodes)
                    {
                        var text = string.Empty;
                        if (node.NodeType == HtmlNodeType.Text || node.NodeType == HtmlNodeType.Comment)
                        {
                            text = node.InnerText;
                        }
                        else
                        {
                            text = node.Name;
                            if (node.HasAttributes)
                            {
                                foreach (var attr in node.Attributes)
                                {
                                    text += nbsp + attr.Name + "=" + attr.Value + nbsp;
                                }
                            }
                        }
                        if (!string.IsNullOrEmpty(text.Trim()))
                        {
                            TreeNodeEx domNode = new TreeNodeEx(text.Trim());
                            domNode.HtmlNode    = node;
                            domNode.ToolTipText = node.XPath;
                            curDomNode.Nodes.Add(domNode);
                            stackDomNode.Push(domNode);
                        }
                    }
                }
            }
            HtmlTree.ExpandAll();
            HtmlTree.EndUpdate();
        }
Exemplo n.º 3
0
        void WebBrower_WBLButtonUp(object sender, csExWB.HTMLMouseEventArgs e)
        {
            if (e.SrcElement != null)
            {
                //user is scrolling using scrollbars
                //if (e.SrcElement.tagName == "HTML")
                //    return;
                //If DIV then we can look for an HTML child element
                //AllForms.m_frmLog.AppendToLog("cEXWB1_WBLButtonUp==>" + e.SrcElement.tagName);
                TreeNodeEx tnRet = null;



                foreach (var tn in HtmlTree.Nodes)
                {
                    var treeNodeEx      = tn as TreeNodeEx;
                    var selectedElement = new SelectedElement();
                    selectedElement.tagName   = e.SrcElement.tagName.ToLower();
                    selectedElement.innerText = e.SrcElement.innerText;
                    tnRet = this.FindNodeExt(treeNodeEx, selectedElement);
                    if (tnRet != null)
                    {
                        break;
                    }
                }
                if (tnRet != null)
                {
                    tnRet.ForeColor = Color.Red;
                    tnRet.Expand();
                    HtmlTree.SelectedNode = tnRet;
                    var sb = new StringBuilder();
                    sb.AppendLine("xpath:" + tnRet.HtmlNode.XPath);
                    sb.AppendLine(HtmlAgilityPackHelper.GetStringByXPath(Html, tnRet.HtmlNode.XPath, "|"));
                    richTextBox.Text += sb.ToString();
                }
            }
            else
            {
                //AllForms.m_frmLog.AppendToLog("cEXWB1_WBLButtonUp");
            }

            //Rectangle rt = new Rectangle(m_mposX - 1, m_mposY - 1, 2, 2);
            //if (rt.Contains(e.ClientX, e.ClientY))
            //{
            //    //AllForms.m_frmLog.AppendToLog("MOUSE CLICKED");
            //}
        }
Exemplo n.º 4
0
        private void btnSearch_Click(object sender, EventArgs e)
        {
            TreeNodeEx tnRet = null;



            foreach (var tn in HtmlTree.Nodes)
            {
                var treeNodeEx = tn as TreeNodeEx;


                tnRet = this.FindNodeExt(treeNodeEx, this.txtUrl.Text);
                if (tnRet != null)
                {
                    break;
                }
            }
            tnRet.ForeColor = Color.Red;
            tnRet.Expand();
        }
Exemplo n.º 5
0
        // 递归
        private TreeNodeEx FindNodeExt(TreeNodeEx tnParent, string strValue)
        {
            if (tnParent == null)
            {
                return(null);
            }
            if (tnParent.HtmlNode.InnerText == strValue)
            {
                return(tnParent);
            }
            TreeNodeEx tnRet = null;

            foreach (var tn in tnParent.Nodes)
            {
                var treeNodeEx = tn as TreeNodeEx;
                tnRet = FindNodeExt(treeNodeEx, strValue);
                if (tnRet != null)
                {
                    break;
                }
            }
            return(tnRet);
        }
Exemplo n.º 6
0
        // 递归
        private TreeNodeEx FindNodeExt(TreeNodeEx tnParent, SelectedElement strValue)
        {
            if (tnParent == null) return null;

            //if (tnParent.HtmlNode.InnerText == strValue) return tnParent;
            if(tnParent.HtmlNode.Name==strValue.tagName)
            {
                if(tnParent.HtmlNode.InnerText==strValue.innerText)
                {
                    return tnParent;
                }
            }

            TreeNodeEx tnRet = null;
            foreach (var tn in tnParent.Nodes)
            {
                var treeNodeEx = tn as TreeNodeEx;

                tnRet = FindNodeExt(treeNodeEx, strValue);
                if (tnRet != null) break;
            }
            return tnRet;
        }
Exemplo n.º 7
0
 // 递归
 private TreeNodeEx FindNodeExt(TreeNodeEx tnParent, string strValue)
 {
     if (tnParent == null) return null;
     if (tnParent.HtmlNode.InnerText==strValue) return tnParent;
     TreeNodeEx tnRet = null;
     foreach (var tn in tnParent.Nodes)
     {
         var treeNodeEx =tn as TreeNodeEx;
         tnRet = FindNodeExt(treeNodeEx, strValue);
         if (tnRet != null) break;
     }
     return tnRet;
 }
Exemplo n.º 8
0
        ///// <summary>
        ///// 生成目录树
        ///// </summary>
        //private void BuildTree()
        //{
        //    FileTree.BeginUpdate();
        //    //存放树节点的栈
        //    Stack<TreeNode> skNode = new Stack<TreeNode>();
        //    int imageIndex = 0;
        //    //添加磁盘列表
        //    List<string> drives = new List<string>();
        //    drives.Add(Directory.GetLogicalDrives().LastOrDefault());
        //    for (int i = 0; i < drives.Count; i++)
        //    {
        //        //每个节点的Text存放目录名,Name存放全路径
        //        TreeNode node = new TreeNode(drives[i], 0, 0);
        //        node.Name = drives[i];
        //        FileTree.Nodes.Add(node);
        //        skNode.Push(node);
        //    }
        //    while (skNode.Count > 0)
        //    {
        //        //弹出栈顶目录,并获取路径
        //        TreeNode curNode = skNode.Pop();
        //        string path = curNode.Name;
        //        FileInfo fInfo = new FileInfo(path);
        //        if ((fInfo.Attributes & FileAttributes.Directory) != 0)
        //        {
        //            string[] subDirs = null;
        //            string[] subFiles = null;
        //            try
        //            {
        //                //获取当前目录下的所有子目录和文件
        //                subDirs = Directory.GetDirectories(path);
        //                subFiles = Directory.GetFiles(path);
        //            }
        //            catch
        //            { }
        //            if (subDirs != null && subFiles != null)
        //            {
        //                //目录入栈
        //                for (int i = 0; i < subDirs.Length; i++)
        //                {
        //                    string dirName = Path.GetFileName(subDirs[i]);
        //                    TreeNode dirNode = new TreeNode(dirName, 1, 1);
        //                    dirNode.Name = subDirs[i];
        //                    curNode.Nodes.Add(dirNode);
        //                    skNode.Push(dirNode);
        //                }
        //                //文件无需入栈
        //                for (int i = 0; i < subFiles.Length; i++)
        //                {
        //                    string fileName = Path.GetFileName(subFiles[i]);
        //                    curNode.Nodes.Add(subFiles[i], fileName, 2);
        //                }
        //            }
        //        }
        //    }
        //    FileTree.EndUpdate();
        //}
        ///// <summary>
        ///// 生成目录树
        ///// </summary>
        //private void BuildTree2()
        //{
        //    FileTree.BeginUpdate();
        //    Stack<DomNode> stackDomNode=new Stack<DomNode>();
        //    foreach (var firstNode in this.htmlDocument.DocumentNode.ChildNodes)
        //    {
        //        DomNode domNode = new DomNode();
        //        string text = firstNode.Name;
        //        if (firstNode.HasAttributes)
        //        {
        //            foreach (var attr in firstNode.Attributes)
        //            {
        //                text += nbsp + attr.Name + "=" + attr.Value + nbsp;
        //            }
        //        }
        //        domNode.TreeNode = new TreeNode(text);
        //        domNode.HtmlNode = firstNode;
        //        FileTree.Nodes.Add(domNode.TreeNode);
        //        stackDomNode.Push(domNode);
        //        RootDomNode = domNode;
        //    }
        //    FileTree.EndUpdate();
        //    while (stackDomNode.Count>0)
        //    {
        //        DomNode curDomNode = stackDomNode.Pop();
        //        if(curDomNode.HtmlNode.ChildNodes.Count>0)
        //        {
        //            foreach (var node in curDomNode.HtmlNode.ChildNodes)
        //            {
        //                DomNode domNode = new DomNode();
        //                string text = node.Name;
        //                if (node.HasAttributes)
        //                {
        //                    foreach (var attr in node.Attributes)
        //                    {
        //                        text += nbsp + attr.Name + "=" + attr.Value + nbsp;
        //                    }
        //                }
        //                domNode.TreeNode = new TreeNode(text);
        //                domNode.HtmlNode = node;
        //                curDomNode.TreeNode.Nodes.Add(domNode.TreeNode);
        //                stackDomNode.Push(domNode);
        //            }
        //        }
        //    }
        //    FileTree.EndUpdate();
        //}
        /// <summary>
        /// 生成目录树
        /// </summary>
        private void BuildTree3()
        {
            HtmlTree.BeginUpdate();
            HtmlTree.Nodes.Clear();
            Stack<TreeNodeEx> stackDomNode = new Stack<TreeNodeEx>();
            foreach (var firstNode in this.htmlDocument.DocumentNode.ChildNodes)
            {

                string text = firstNode.Name;
                if (firstNode.HasAttributes)
                {
                    foreach (var attr in firstNode.Attributes)
                    {
                        text += nbsp + attr.Name + "=" + attr.Value + nbsp;
                    }
                }
                TreeNodeEx domNode = new TreeNodeEx(text);

                domNode.HtmlNode = firstNode;
                HtmlTree.Nodes.Add(domNode);
                stackDomNode.Push(domNode);
            }

            while (stackDomNode.Count > 0)
            {
                TreeNodeEx curDomNode = stackDomNode.Pop();
                if (curDomNode.HtmlNode.ChildNodes.Count > 0)
                {
                    foreach (var node in curDomNode.HtmlNode.ChildNodes)
                    {
                        var text = string.Empty;
                        if (node.NodeType == HtmlNodeType.Text || node.NodeType == HtmlNodeType.Comment)
                        {
                            text = node.InnerText;
                        }
                        else
                        {
                            text = node.Name;
                            if (node.HasAttributes)
                            {
                                foreach (var attr in node.Attributes)
                                {
                                    text += nbsp + attr.Name + "=" + attr.Value + nbsp;
                                }
                            }
                        }
                        if(!string.IsNullOrEmpty(text.Trim()))
                        {
                            TreeNodeEx domNode = new TreeNodeEx(text.Trim());
                            domNode.HtmlNode = node;
                            domNode.ToolTipText = node.XPath;
                            curDomNode.Nodes.Add(domNode);
                            stackDomNode.Push(domNode);
                        }

                    }
                }
            }
            HtmlTree.ExpandAll();
            HtmlTree.EndUpdate();
        }