How can I write out decoded HTML using HTMLAgilityPack?

c# decode html html-agility-pack innertext


I am having partial success in my attempt to write HTML to a DOCX file using HTMLAgilityPack and the DOCX library. However, the text I'm inserting into the .docx file contains encoded html such as:

La ciudad de Los Ángeles (California) ha sincronizado su red completa de semáforos —casi 4.500—, que cubre una zona de 1.215 kilómetros cuadrados (469 millas cuadradas). Según el diario

What I want it to be is more like this:

La ciudad de Los Angeles (California) ha sincronizado su red completa de semaforos - casi 4.500 -, que cubre una zona de 1.215 kilometros cuadrados (469 millas
cuadradas). Segun el diario

To show some context, this is the code I'm using:

private void ParseHTMLAndConvertBackToDOCX()
    List<string> sourceText = new List<string>();
    List<string> targetText = new List<string>();
    HtmlAgilityPack.HtmlDocument htmlDocSource = new HtmlAgilityPack.HtmlDocument();
    HtmlAgilityPack.HtmlDocument htmlDocTarget = new HtmlAgilityPack.HtmlDocument();

    // There are various options, set as needed
    htmlDocSource.OptionFixNestedTags = true;
    htmlDocTarget.OptionFixNestedTags = true;


    // Popul8 generic list of string with source text lines
    if (htmlDocSource.DocumentNode != null)
        IEnumerable<HtmlAgilityPack.HtmlNode> pNodes = htmlDocSource.DocumentNode.SelectNodes("//text()");

        foreach (HtmlNode sText in pNodes)
            if (!string.IsNullOrWhiteSpace(sText.InnerText))

. . .

The most pertinent line is no doubt:


Should it be something other than InnerText?

Is it possible to to something like:



Intellisense is not working with this, even though the project compiles and runs; trying to see what other options there are besides InnerText for HTMLNode is thus fruitless; I know there's OuterText, InnerHTML, and OuterHMTL, though...

2/18/2014 1:53:15 AM

Accepted Answer

Try with:



2/18/2014 2:11:10 AM

Popular Answer

You can use HtmlEntity.DeEntitize(sText.InnerText) from HTMLAgilityPack.

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow