示例#1
0
        void RecursionHtmlNode(StrTreeNode treeNode, INode htmlNode, bool siblingRequired)
        {
            if (htmlNode == null || treeNode == null)
            {
                return;
            }

            StrTreeNode current = treeNode;

            //current node
            if (htmlNode is ITag)
            {
                string        nodeString = "";
                ITag          tag        = (htmlNode as ITag);
                string[]      values     = null;
                HtmlTagType[] types      = GetFullTagInfo(tag, out values);
                if (!tag.IsEndTag())
                {
                    for (int i = 0; i < types.Length; ++i)
                    {
                        if (null != OnReadTagBegin)
                        {
                            OnReadTagBegin(types[i], values[i]);
                        }
                        nodeString += types[i].ToString() + "={" + values[i] + "} ";
                    }
                    //if (tag.Attributes != null && tag.Attributes.Count > 0)
                    //{
                    //    if (tag.Attributes["ID"] != null)
                    //    {
                    //        if (null != OnReadTagBegin)
                    //            OnReadTagBegin(tag.TagName, "ID", tag.Attributes["ID"].ToString());
                    //        nodeString = nodeString + " { id=\"" + tag.Attributes["ID"].ToString() + "\" }";
                    //    }
                    //    if (tag.Attributes["HREF"] != null)
                    //    {
                    //        if (null != OnReadTagBegin)
                    //            OnReadTagBegin(tag.TagName, "HREF", tag.Attributes["HREF"].ToString());
                    //        nodeString = nodeString + " { href=\"" + tag.Attributes["HREF"].ToString() + "\" }";
                    //    }
                    //    if (tag.Attributes["SIZE"] != null)
                    //    {
                    //        if (null != OnReadTagBegin)
                    //            OnReadTagBegin(tag.TagName, "SIZE", tag.Attributes["SIZE"].ToString());
                    //        nodeString = nodeString + " { size=\"" + tag.Attributes["SIZE"].ToString() + "\" }";
                    //    }
                    //    if (tag.Attributes["COLOR"] != null)
                    //    {
                    //        if (null != OnReadTagBegin)
                    //            OnReadTagBegin(tag.TagName, "COLOR", tag.Attributes["COLOR"].ToString());
                    //        nodeString = nodeString + " { color=\"" + tag.Attributes["COLOR"].ToString() + "\" }";
                    //    }
                    //}
                }
                else
                {
                    for (int i = 0; i < types.Length; ++i)
                    {
                        if (null != OnReadTagEnd)
                        {
                            OnReadTagEnd(types[i]);
                        }
                    }
                    nodeString = tag.TagName + " End";
                }
                current = treeNode.AddItem(nodeString);
            }
            //获取节点间的内容
            if (htmlNode.Children != null && htmlNode.Children.Count > 0)
            {
                RecursionHtmlNode(current, htmlNode.FirstChild, true);
                //content = htmlNode.FirstChild.GetText();
                //节点列表.Add(content);
            }
            else if (!(htmlNode is ITag))
            {
                if (htmlNode is IText)
                {
                    IText  tex        = htmlNode as IText;
                    string nodeString = tex.GetText();
                    nodeString = nodeString.Replace("\r\n", "");
                    nodeString = nodeString.Replace("&nbsp;", " ");
                    nodeString = nodeString.Replace("\t", "    ");
                    byte[] utf8Bom = new byte[] { 239, 187, 191 };
                    nodeString = nodeString.Replace(PlatformTools.BS2String_UTF8(utf8Bom), "");
                    bool allIsSpace = true;
                    for (int i = 0; i < nodeString.Length; ++i)
                    {
                        if (nodeString[i] != ' ')
                        {
                            allIsSpace = false;
                            break;
                        }
                    }
                    if (!string.IsNullOrEmpty(nodeString) && !allIsSpace)
                    {
                        if (null != OnReadText)
                        {
                            OnReadText(nodeString);
                        }
                        current = treeNode.AddItem(nodeString);
                    }
                }
                else
                {
                    string typestr = htmlNode.GetType().ToString();
                    if (null != OnReadText)
                    {
                        OnReadText(typestr);
                    }
                    current = treeNode.AddItem(typestr);
                }
            }

            //the sibling nodes
            if (siblingRequired)
            {
                INode sibling = htmlNode.NextSibling;
                while (sibling != null)
                {
                    RecursionHtmlNode(current, sibling, false);
                    sibling = sibling.NextSibling;
                }
            }
        }