Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I write out decoded HTML using HTMLAgilityPack?

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

like image 486
B. Clay Shannon-B. Crow Raven Avatar asked Feb 18 '14 01:02

B. Clay Shannon-B. Crow Raven


2 Answers

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

like image 108
MattheW Avatar answered Nov 15 '22 12:11

MattheW


Try with:

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

Examples

like image 38
Felipe Pereira Avatar answered Nov 15 '22 14:11

Felipe Pereira