HtmlAgilityPack.HtmlNode.CloneNode C# (CSharp) Method

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
		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;
			}

			// attributes
			if (HasAttributes)
			{
				foreach(HtmlAttribute att in _attributes)
				{
					HtmlAttribute newatt = att.Clone();
					node.Attributes.Append(newatt);
				}
			}

			// closing attributes
			if (HasClosingAttributes)
			{
				node._endnode = _endnode.CloneNode(false);
				foreach(HtmlAttribute att in _endnode._attributes)
				{
					HtmlAttribute newatt = att.Clone();
					node._endnode._attributes.Append(newatt);
				}
			}
			if (!deep)
			{
				return node;
			}

			if (!HasChildNodes)
			{
				return node;
			}

			// child nodes
			foreach(HtmlNode child in _childnodes)
			{
				HtmlNode newchild = child.Clone();
				node.AppendChild(newchild);
			}
			return node;
		}

Same methods

HtmlNode::CloneNode ( string newName ) : HtmlNode
HtmlNode::CloneNode ( string newName, bool deep ) : HtmlNode

Usage Example

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);
        }
All Usage Examples Of HtmlAgilityPack.HtmlNode::CloneNode