Reading iso-8859-1 rss feed C# WP7

谁都会走 提交于 2019-12-05 17:16:30

You can specify an encoding by setting encoding before calling client.DownloadStringAsync:

webClient.Encoding = Encoding.GetEncoding("iso-8859-1")

In your code sample you do not create the XML doc anywhere. Are some code missing? You should initialize it with something like:

var xml = XDocument.Load((string)args.Result);

If it helps, you can use:

    var myString = HttpUtility.HtmlDecode(feeditem.description);

This way every special character will be decode, you can then display myString correctly

Windows Phone 7 and Silverlight does not support other encodings such as ISO-8859-1, they only support ASCII and the Unicode encoders. For anything else you will need to use OpenReadAsync to get a stream of bytes then apply your own implementation of an encoding.

This blog might be helpful to you in creating one.

ISO-8859-1 most definitely is supported in WP7. It is the only one of the ISO-8859-* encodings that is. I use an XmlReader to deserialize RSS streams and UTF-* and ISO-8859-1 are the only encodings that are supported by that class (windows-* and ISO-8859-2 and above throw exceptions in the XmlReader c'tor).

Try using an XmlReader like this (without specifying the encoding):

 using (XmlReader reader = XmlReader.Create(stream))
 {
     ...
 }

The XmlReader will get the encoding from the xml declaration in the stream.

You may still have problems displaying the upper half of the characters (above 0x80). I had this problem in feed me (my WP7 app) and used this little hack to fix things up:

    public static string EncodeHtml(string text)
    {
        if (text == null) return string.Empty;

        StringBuilder decodedText = new StringBuilder();
        foreach (char value in text)
        {
            int i = (int)value;
            if (i > 127)
            {
                decodedText.Append(string.Format("&#{0};", i));
            }
            else
            {
                decodedText.Append(value);
            }
        }
        return decodedText.ToString();
    }

It only works in a WebBrowser control of course, but that is the only place that I ever saw an incorrect display.

Hope this helps, Calum

This worked for me when needing to decode the rss xml. It's generic enough so that it will support all encryption types supported by .NET

        WebClient wcRSSFeeds = new WebClient();
        String rssContent;

        // Support for international chars
        Encoding encoding = wcRSSFeeds.Encoding;
        if (encoding != null)
        {
            encoding = Encoding.GetEncoding(encoding.BodyName);
        }
        else
        {
            encoding = Encoding.UTF8;  // set to standard if none given 
        }
        Stream stRSSFeeds = wcRSSFeeds.OpenRead(feedURL); // feedURL is a string eg, "http://blah.com"

        using (StreamReader srRSSFeeds = new StreamReader(stRSSFeeds, encoding, false))
        {
            rssContent = srRSSFeeds.ReadToEnd();
        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!