HtmlAgilityPack.HtmlNode.WriteTo C# (CSharp) Method

WriteTo() public method

Saves the current node to the specified TextWriter.
public WriteTo ( TextWriter outText ) : void
outText System.IO.TextWriter The TextWriter to which you want to save.
return void
		public void WriteTo(TextWriter outText)
		{
			string html;
			switch(_nodetype)
			{
				case HtmlNodeType.Comment:
					html = ((HtmlCommentNode)this).Comment;
					if (_ownerdocument.OptionOutputAsXml)
					{
						outText.Write("<!--" + GetXmlComment((HtmlCommentNode)this) + " -->");
					}
					else
					{
						outText.Write(html);
					}
					break;

				case HtmlNodeType.Document:
					if (_ownerdocument.OptionOutputAsXml)
					{
						outText.Write("<?xml version=\"1.0\" encoding=\"" + _ownerdocument.GetOutEncoding().BodyName + "\"?>");

						// check there is a root element
						if (_ownerdocument.DocumentNode.HasChildNodes)
						{
							int rootnodes = _ownerdocument.DocumentNode._childnodes.Count;
							if (rootnodes > 0)
							{
								HtmlNode xml = _ownerdocument.GetXmlDeclaration();
								if (xml != null)
								{
									rootnodes --;
								}

								if (rootnodes > 1)
								{
									if (_ownerdocument.OptionOutputUpperCase)
									{
										outText.Write("<SPAN>");
										WriteContentTo(outText);
										outText.Write("</SPAN>");
									}
									else
									{
										outText.Write("<span>");
										WriteContentTo(outText);
										outText.Write("</span>");
									}
									break;
								}
							}
						}
					}
					WriteContentTo(outText);
					break;

				case HtmlNodeType.Text:
					html = ((HtmlTextNode)this).Text;
					if (_ownerdocument.OptionOutputAsXml)
					{
						outText.Write(HtmlDocument.HtmlEncode(html));
					}
					else
					{
						outText.Write(html);
					}
					break;

				case HtmlNodeType.Element:
					string name;
					if (_ownerdocument.OptionOutputUpperCase)
					{
						name = Name.ToUpper();
					}
					else
					{
						name = Name;
					}

					if (_ownerdocument.OptionOutputAsXml)
					{
						if (name.Length > 0)
						{
							if (name[0] == '?')
							{
								// forget this one, it's been done at the document level
								break;
							}

							if (name.Trim().Length == 0)
							{
								break;
							}
							name = HtmlDocument.GetXmlName(name);
						}
						else
						{
							break;
						}
					}

					outText.Write("<" + name);
					WriteAttributes(outText, false);

					if (!HasChildNodes)
					{
						if (HtmlNode.IsEmptyElement(Name))
						{
							if ((_ownerdocument.OptionWriteEmptyNodes) || (_ownerdocument.OptionOutputAsXml))
							{
								outText.Write(" />");
							}
							else
							{
								if (Name.Length > 0)
								{
									if (Name[0] == '?')
									{
										outText.Write("?");
									}
								}

								outText.Write(">");
							}
						}
						else
						{
							outText.Write("></" + name + ">");
						}
					}
					else
					{
						outText.Write(">");
						bool cdata = false;
						if (_ownerdocument.OptionOutputAsXml)
						{
							if (HtmlNode.IsCDataElement(Name))
							{
								// this code and the following tries to output things as nicely as possible for old browsers.
								cdata = true;
								outText.Write("\r\n//<![CDATA[\r\n");
							}
						}

						if (cdata)
						{
							if (HasChildNodes)
							{
								// child must be a text
								ChildNodes[0].WriteTo(outText);
							}
							outText.Write("\r\n//]]>//\r\n");
						}
						else
						{
							WriteContentTo(outText);
						}

						outText.Write("</" + name);
						if (!_ownerdocument.OptionOutputAsXml)
						{
							WriteAttributes(outText, true);
						}
						outText.Write(">");
					}
					break;
			}
		}

Same methods

HtmlNode::WriteTo ( ) : string
HtmlNode::WriteTo ( XmlWriter writer ) : void

Usage Example

    /// <summary>
    /// Saves the <see cref="HtmlDocument"/> to the specified path.
    /// </summary>
    /// <param name="path">The file to write to.</param>
    /// <param name="node">The <see cref="HtmlNode"/> to save.</param>
    /// <param name="encoding">The encoding to use when writing the file.</param>
    /// <seealso cref="HtmlNode"/>
    /// <see cref="HtmlDocument"/>
    public static void Save(string path, HtmlNode node, Encoding encoding)
    {
        if (path == null || node == null)
        {
            DebugBreakOrThrow("Figure out why " + (path == null ? "path" : "node") + "is null", new ArgumentNullException(path == null ? "path" : "node"));
        }

        // Will only be triggered if the caller isn't called by another Save().
        if (encoding == null)
        {
            DebugBreakOrThrow("Figure out why encoding is null.", new ArgumentNullException("encoding"));
        }

        using (FileStream fileStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read, 1024))
        {
            using (StreamWriter streamWriter = new StreamWriter(fileStream, encoding))
            {
                // StreamWriter is a TextWriter, so we can pass it to HtmlNode.WriteContentTo(TextWriter).
                // So node.WriteTo only saves the current node, which is only useful if the node has no children.
                // node.WriteContentTo, only saves the current nodes children, therefore if the current node has a parent, we use the parent to save.
                // this will include all siblings aswell :S
                if (node.ParentNode != null)
                {
                    node.ParentNode.WriteContentTo(streamWriter);
                }
                else if (node.HasChildNodes == false)
                {
                    node.WriteTo(streamWriter);
                }
                else if (node.Name == HtmlNode.HtmlNodeTypeNameDocument)
                {
                    node.WriteContentTo(streamWriter);
                }
                else
                {
                    // TODO: Properly save parent-less node with children.
                    DebugBreakOrThrow("Properly save parent-less node with children. Inspect 'node'.", new InvalidOperationException("Don't know how to save the node, and it's children!"));
                }

                streamWriter.Flush();
            }
        }
    }
All Usage Examples Of HtmlAgilityPack.HtmlNode::WriteTo