Labels

slider

Recent

Navigation

Learn HAP: HTML Writer using Agility Pack C#

HTML Writer using Agility Pack C#, htmlagilitypack write to string, html agility pack c# example, html write html agility pack, htmlagilitypack write html

Introduction

In the earlier session HTML Traversing html using Agility Pack C# we have seen that the HTML DOM can be accessed in various ways according to our requirements. Sometimes, it is necessary to store the content that we read and then, manipulate upon it. Exactly, to help us in these scenarios HAP provides html write html agility pack which is powerful yet simple to understand and use. There are few important methods of HTML Writer using Agility Pack C# that one should know to take advantage for an efficient usage of the HTML DOM.

HTML Writer using Agility Pack C#

Below mentioned are the essential parts of htmlagilitypack write html. Do go through them thoroughly for a better understanding about using such magnificent features from HAP.

Free Video Library: Learn HTML Agility Pack Step by Step

Html Writer

This is the tool that helps us save the given HTML DOM as HTML Document or you have a choice to manipulate the Node alone. So, on the whole, you have two objectives you can achieve:

  1. Saving HTMLDocument
  2. Write HtmlNode

In this session, we will just pertain to the methods that help us in Saving HTML Document as per our needs.

# 1 Save (Stream)

This public member of the HtmlAgilityPack.HtmlDocument saves the HTML document to the stream you mention to it. Stream could be a File or any other that can be considered as a stream. It takes the stream as an argument where the stream is the one you intend to write your HTML Document to. The method returns none and therefore is of void type. Find the below html agility pack c# example that gives an insight about the usage of this method.
 
var html =
        @"<!DOCTYPE html>
<html>
<body>
 <h1>Saves the HTML document to the specified stream</h1>
 <p>Technology Crowds</p>
 <h2>www.TechnologyCrowds</h2>
</body>
</html>";

        var htmlDoc = new HtmlDocument();
        htmlDoc.LoadHtml(html);
 FileStream sw = new FileStream("TC.html", FileMode.Create);

        htmlDoc.Save(sw);
 sw.Close();
        var htmlNewDoc = new HtmlDocument();
        htmlNewDoc.Load("TC.html");
        var node = htmlNewDoc.DocumentNode.SelectSingleNode("//body");

        Console.WriteLine(node.OuterHtml);

# 2 Save (StreamWriter)

The method here being described, interestingly, saves your given HTML Document to the StreamWriter you specify. There is a single parameter to this method which is writer that represents the StreamWriter you desire to act upon. You have the privilege to access this save method from anywhere as it is a public method but returns none as it is declared to be a void type.
 
var html =
        @"<!DOCTYPE html>
  <html>
  <body>
  <h1>Saves the HTML document</h1>
  <p>Technology Crowds</p>
  <h2>www.TechnologyCrowds</h2>
  </body>
  </html>";

        var htmlDoc = new HtmlDocument();
        htmlDoc.LoadHtml(html);
  
 StreamWriter sw = new StreamWriter("TCFileStream.html");
        htmlDoc.Save(sw);
        sw.Close();
  
        var htmlNewDoc = new HtmlDocument();
        htmlNewDoc.Load("TCFileStream.html");
        var node = htmlNewDoc.DocumentNode.SelectSingleNode("//body");
        Console.WriteLine(node.OuterHtml);

# 3 Save (TextWriter)

This method gives you the ability to save the HTML document to the text writer ylu specify. It is a member of HtmlAgilityPack.HtmlDocument and accepts parameter of type text writer which is actually the one that you intend to save to! It being a public member is easily accessible from anywhere. The code here explains how to use this method.
 
var html =
        @"<!DOCTYPE html>
  <html>
  <body>
  <h1>Saves the HTML document</h1>
  <p>Technology Crowds</p>
  <h2>www.TechnologyCrowds</h2>
  </body>
  </html>";

        var htmlDoc = new HtmlDocument();
        htmlDoc.LoadHtml(html);
  
 TextWriter tw = File.CreateText("TCTextWriter.html");
        htmlDoc.Save(tw);
        tw.Close();

        var htmlNewDoc = new HtmlDocument();
        htmlNewDoc.Load("TCTextWriter.html");
        var node = htmlNewDoc.DocumentNode.SelectSingleNode("//body");
        Console.WriteLine(node.OuterHtml);

# 4 Save (String)

Most of the times you would be finding a situation where HTML Document has to be manipulated not necessarily now but sometimes later and in such cases saving the document in writer or stream would be meaningless. To meet these needs, HAP provides the ability to save the HTML document to the file and then you can use it later to manipulate using file operations. The save method that accepts the name of the file in the form of string as parameter is a public method that saves the given HTML document to that file.
 
var html =
        @"<!DOCTYPE html>
  <html>
  <body>
  <h1>Saves the HTML document to the specified stream</h1>
  <p>Technology Crowds</p>
  <h2>www.TechnologyCrowds</h2>
  </body>
  </html>";

        var htmlDoc = new HtmlDocument();
        htmlDoc.LoadHtml(html);
  
 htmlDoc.Save("TCtest.html");
        var htmlNewDoc = new HtmlDocument();
        htmlNewDoc.Load("TCtest.html");

        var node = htmlNewDoc.DocumentNode.SelectSingleNode("//body");
        Console.WriteLine(node.OuterHtml);

# 5 Save (XmlWriter)

When we desire to write or save the Html document to XmlWriter, then this method is going to be best bet. This public member takes the XmlWriter that you want to save, as a parameter. Sample code below explains its usage.
 
var html =
        @"<!DOCTYPE html>
  <html>
  <body>
  <h1>Saves the HTML document</h1>
  <p>Technology Crowds</p>
  <h2>www.TechnologyCrowds</h2>
  </body>
  </html>";

        var htmlDoc = new HtmlDocument();
        htmlDoc.LoadHtml(html);
  
  htmlDoc.OptionOutputAsXml = true;
        StringWriter sw = new StringWriter();
        XmlTextWriter xw = new XmlTextWriter(sw);
        htmlDoc.Save(xw);
  
        string result = sw.ToString();
        Console.WriteLine(result);

# 6 Save (Stream, Encoding)

At times we will have situations where it is necessary to store particular Html document onto a stream in 'UTF-8' encoding. Save method should be the tool for you that helps in this aspect. All you have to do is to pass a stream type object along with the encoding type as parameters. It is mandatory to pass values that are not null to this public method. Take a sneak below for an idea about how it deals with the requirements of this type.
 
var html =
        @"<!DOCTYPE html>
  <html>
  <body>
  <h1>Saves the HTML document</h1>
  <p>Technology Crowds</p>
  <h2>www.TechnologyCrowds</h2>
  </body>
  </html>";

        var htmlDoc = new HtmlDocument();
        htmlDoc.LoadHtml(html);
  
 htmlDoc.Save("TC.html", Encoding.UTF8);
        var htmlNewDoc = new HtmlDocument();
        htmlNewDoc.Load("TC.html");

        var node = htmlNewDoc.DocumentNode.SelectSingleNode("//body");
        Console.WriteLine(node.OuterHtml);

Relevant Reading

Share

Anjan kant

Outstanding journey in Microsoft Technologies (ASP.Net, C#, SQL Programming, WPF, Silverlight, WCF etc.), client side technologies AngularJS, KnockoutJS, Javascript, Ajax Calls, Json and Hybrid apps etc. I love to devote free time in writing, blogging, social networking and adventurous life

Post A Comment:

0 comments: