C# and HtmlAgilityPack encoding problem

Viewed 42636
WebClient GodLikeClient = new WebClient();
HtmlAgilityPack.HtmlDocument GodLikeHTML = new HtmlAgilityPack.HtmlDocument();

GodLikeHTML.Load(GodLikeClient.OpenRead("www.alfa.lt");

So this code returns: "Skaitytojo klausimas psichologui: kas lemia homoseksualumÄ…? - Naujienų portalas Alfa.lt" instead of "Skaitytojo klausimas psichologui: kas lemia homoseksualumą? - Naujienų portalas Alfa.lt".

This webpage is encoded in 1257 (baltic), but textBox1.Text = GodLikeHTML.DocumentNode.OuterHtml; returns the distorted text - baltic diacritics are transformed into some weird several characters long strings :(

And yes, I've tried the HtmlAgilityPack forums. They do suck.

P.S. I'm no programmer, but I work on a community project and I really need to get this code working. Thanks ;}

8 Answers

This seemed to remove the need to know anything about encoding for me:

using System;
using HtmlAgilityPack;
using System.Net;
using System.IO;


    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter the url to pull html documents from: ");

            string url = Console.ReadLine();

            HtmlDocument document = new HtmlDocument();

            var request = WebRequest.Create(url);
            var response = request.GetResponse();

            using (var reader = new StreamReader(response.GetResponseStream()))
            {
                document.LoadHtml(reader.ReadToEnd());
            } 
        }
    }
Related