How can I write out decoded HTML using HTMLAgilityPack?

c# decode html html-agility-pack innertext

Question

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;

    htmlDocSource.Load(sourceHTMLFilename);
    htmlDocTarget.Load(targetHTMLFilename);

    // 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))
            {
                sourceText.Add(sText.InnerText);
            }
        }
    }

. . .

The most pertinent line is no doubt:

sourceText.Add(sText.InnerText);

Should it be something other than InnerText?

Is it possible to to something like:

sourceText.Add(sText.InnerText.Decode());

?

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...

Accepted Answer

Try with:

sourceText.Add(HttpUtility.HtmlDecode(myEncodedString));

Examples


Popular Answer

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



Related

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