CloneNode() public method

Creates a duplicate of the node.
public CloneNode ( bool deep ) : HtmlNode
deep bool true to recursively clone the subtree under the specified node; false to clone only the node itself.
return HtmlNode
Ejemplo n.º 1
0
        internal void InternalTrace(object Value)
        {
            if (!Trace)
            {
                return;
            }
            string     name = null;
            StackFrame sf   = new StackFrame(1, true);

            name = sf.GetMethod().Name;
            string nodename;

            if (_currentnode == null)
            {
                nodename = "(null)";
            }
            else
            {
                nodename = _currentnode.Name;
            }
            string nodevalue;

            if (_currentnode == null)
            {
                nodevalue = "(null)";
            }
            else
            {
                switch (_currentnode.NodeType)
                {
                case HtmlNodeType.Comment:
                    nodevalue = ((HtmlCommentNode)_currentnode).Comment;
                    break;

                case HtmlNodeType.Document:
                    nodevalue = "";
                    break;

                case HtmlNodeType.Text:
                    nodevalue = ((HtmlTextNode)_currentnode).Text;
                    break;

                default:
                    nodevalue = _currentnode.CloneNode(false).OuterHtml;
                    break;
                }
            }
            System.Diagnostics.Trace.WriteLine("oid=" + GetHashCode()
                                               + ",n=" + nodename
                                               + ",a=" + _attindex + ","
                                               + ",v=" + nodevalue + ","
                                               + Value, "N!" + name);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Moves to the next sibling of the current node.
 /// </summary>
 /// <returns>true if the navigator is successful moving to the next sibling node, false if there are no more siblings or if the navigator is currently positioned on an attribute node. If false, the position of the navigator is unchanged.</returns>
 public override bool MoveToNext()
 {
     if (_currentnode.NextSibling == null)
     {
         InternalTrace(">false");
         return(false);
     }
     InternalTrace("_c=" + _currentnode.CloneNode(false).OuterHtml);
     InternalTrace("_n=" + _currentnode.NextSibling.CloneNode(false).OuterHtml);
     _currentnode = _currentnode.NextSibling;
     InternalTrace(">true");
     return(true);
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Clone and entitize an HtmlNode. This will affect attribute values and nodes' text. It will also entitize all child nodes.
        /// </summary>
        /// <param name="node">The node to entitize.</param>
        /// <returns>An entitized cloned node.</returns>
        public static HtmlNode Entitize(HtmlNode node)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }
            HtmlNode result = node.CloneNode(true);

            if (result.HasAttributes)
            {
                Entitize(result.Attributes);
            }

            if (result.HasChildNodes)
            {
                Entitize(result.ChildNodes);
            }
            else
            {
                if (result.NodeType == HtmlNodeType.Text)
                {
                    ((HtmlTextNode)result).Text = Entitize(((HtmlTextNode)result).Text, true, true);
                }
            }
            return(result);
        }
Ejemplo n.º 4
0
        public static HtmlNode Entitize(HtmlNode node)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }
            HtmlNode htmlNode = node.CloneNode(true);

            if (htmlNode.HasAttributes)
            {
                HtmlEntity.Entitize(htmlNode.Attributes);
            }
            if (htmlNode.HasChildNodes)
            {
                HtmlEntity.Entitize(htmlNode.ChildNodes);
            }
            else
            {
                if (htmlNode.NodeType == HtmlNodeType.Text)
                {
                    ((HtmlTextNode)htmlNode).Text = HtmlEntity.Entitize(((HtmlTextNode)htmlNode).Text, true, true);
                }
            }
            return(htmlNode);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Moves to the next sibling of the current node.
        /// </summary>
        /// <returns>true if the navigator is successful moving to the next sibling node, false if there are no more siblings or if the navigator is currently positioned on an attribute node. If false, the position of the navigator is unchanged.</returns>
        public override bool MoveToNext()
        {
            if (_currentnode.NextSibling == null)
            {
#if TRACE_NAVIGATOR
                InternalTrace(">false");
#endif
                return(false);
            }

#if TRACE_NAVIGATOR
            InternalTrace("_c=" + _currentnode.CloneNode(false).OuterHtml);
            InternalTrace("_n=" + _currentnode.NextSibling.CloneNode(false).OuterHtml);
#endif
            _currentnode = _currentnode.NextSibling;
#if TRACE_NAVIGATOR
            InternalTrace(">true");
#endif
            return(true);
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Gets a given node from the list.
 /// </summary>
 public int this[HtmlNode node]
 {
     get
     {
         int index = GetNodeIndex(node);
         if (index == -1)
         {
             throw new ArgumentOutOfRangeException("node", "Node \"" + node.CloneNode(false).OuterHtml + "\" was not found in the collection");
         }
         return(index);
     }
 }
Ejemplo n.º 7
0
        public HtmlNode CloneNode(bool deep)
        {
            HtmlNode node = _ownerdocument.CreateNode(_nodetype);

            node.Name = Name;
            switch (_nodetype)
            {
            case HtmlNodeType.Comment:
                ((HtmlCommentNode)node).Comment = ((HtmlCommentNode)this).Comment;
                return(node);

            case HtmlNodeType.Text:
                ((HtmlTextNode)node).Text = ((HtmlTextNode)this).Text;
                return(node);
            }
            if (HasAttributes)
            {
                foreach (HtmlAttribute attribute in (IEnumerable <HtmlAttribute>)_attributes)
                {
                    HtmlAttribute newAttribute = attribute.Clone();
                    node.Attributes.Append(newAttribute);
                }
            }
            if (HasClosingAttributes)
            {
                node._endnode = _endnode.CloneNode(false);
                foreach (HtmlAttribute attribute3 in (IEnumerable <HtmlAttribute>)_endnode._attributes)
                {
                    HtmlAttribute attribute4 = attribute3.Clone();
                    node._endnode._attributes.Append(attribute4);
                }
            }
            if (deep)
            {
                if (!HasChildNodes)
                {
                    return(node);
                }
                foreach (HtmlNode node2 in (IEnumerable <HtmlNode>)_childnodes)
                {
                    HtmlNode newChild = node2.Clone();
                    node.AppendChild(newChild);
                }
            }
            return(node);
        }
Ejemplo n.º 8
0
        public static HtmlNode Entitize(HtmlNode node)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }
            HtmlNode node2 = node.CloneNode(true);

            if (node2.HasAttributes)
            {
                Entitize(node2.Attributes);
            }
            if (node2.HasChildNodes)
            {
                Entitize(node2.ChildNodes);
                return(node2);
            }
            if (node2.NodeType == HtmlNodeType.Text)
            {
                ((HtmlTextNode)node2).Text = Entitize(((HtmlTextNode)node2).Text, true, true);
            }
            return(node2);
        }
Ejemplo n.º 9
0
        internal void InternalTrace(object traceValue)
        {
            string outerHtml;

            if (!Trace)
            {
                return;
            }
            StackFrame frame = new StackFrame(1, true);
            string     name  = frame.GetMethod().Name;
            string     str2  = (_currentnode == null) ? "(null)" : _currentnode.Name;

            if (_currentnode == null)
            {
                outerHtml = "(null)";
            }
            else
            {
                switch (_currentnode.NodeType)
                {
                case HtmlNodeType.Document:
                    outerHtml = "";
                    goto Label_00AE;

                case HtmlNodeType.Comment:
                    outerHtml = ((HtmlCommentNode)_currentnode).Comment;
                    goto Label_00AE;

                case HtmlNodeType.Text:
                    outerHtml = ((HtmlTextNode)_currentnode).Text;
                    goto Label_00AE;
                }
                outerHtml = _currentnode.CloneNode(false).OuterHtml;
            }
            Label_00AE :;
            HtmlAgilityPack.Trace.WriteLine(string.Format("oid={0},n={1},a={2},v={3},{4}", new object[] { GetHashCode(), str2, _attindex, outerHtml, traceValue }), "N!" + name);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Clone and entitize an HtmlNode. This will affect attribute values and nodes' text. It will also entitize all child nodes.
        /// </summary>
        /// <param name="node">The node to entitize.</param>
        /// <returns>An entitized cloned node.</returns>
        public static HtmlNode Entitize(HtmlNode node)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }
            HtmlNode result = node.CloneNode(true);
            if (result.HasAttributes)
                Entitize(result.Attributes);

            if (result.HasChildNodes)
            {
                Entitize(result.ChildNodes);
            }
            else
            {
                if (result.NodeType == HtmlNodeType.Text)
                {
                    ((HtmlTextNode) result).Text = Entitize(((HtmlTextNode) result).Text, true, true);
                }
            }
            return result;
        }
Ejemplo n.º 11
0
 /// <summary>
 /// Gets a given node from the list.
 /// </summary>
 public int this[HtmlNode node]
 {
     get
     {
         int index = GetNodeIndex(node);
         if (index == -1)
         {
             throw new ArgumentOutOfRangeException("node", "Node \"" + node.CloneNode(false).OuterHtml + "\" was not found in the collection");
         }
         return index;
     }
 }
Ejemplo n.º 12
0
 /// <summary>
 /// Clone and entitize an HtmlNode. This will affect attribute values and nodes' text. It will also entitize all child nodes.
 /// 
 /// </summary>
 /// <param name="node">The node to entitize.</param>
 /// <returns>
 /// An entitized cloned node.
 /// </returns>
 public static HtmlNode Entitize(HtmlNode node)
 {
   if (node == null)
     throw new ArgumentNullException("node");
   HtmlNode htmlNode = node.CloneNode(true);
   if (htmlNode.HasAttributes)
     HtmlEntity.Entitize(htmlNode.Attributes);
   if (htmlNode.HasChildNodes)
     HtmlEntity.Entitize(htmlNode.ChildNodes);
   else if (htmlNode.NodeType == HtmlNodeType.Text)
     ((HtmlTextNode) htmlNode).Text = HtmlEntity.Entitize(((HtmlTextNode) htmlNode).Text, true, true);
   return htmlNode;
 }
Ejemplo n.º 13
0
        /**********************************************************************/
        private void SectionizeNodes(HtmlNode node, ref List<DocumentSection> documentSections)
        {
            foreach (HtmlAttribute hat in node.Attributes)
            {
                if (hat.Name == "id" && !string.IsNullOrEmpty(hat.Value))
                {
                    DocumentSection ds = new DocumentSection();
                    ds.SectionNode = node.CloneNode(true);
                    ;
                    ds.title = hat.Value;
                    documentSections.Add(ds);
                }
            }

            foreach (HtmlNode child in node.ChildNodes)
            {
                SectionizeNodes(child, ref documentSections);
            }
        }