Reading iso-8859-1 c # wp7 RSS feed

I am trying to read an rss feed that uses iso-8859-1 encoding.

I can get all the elements in order, the problem is when I put it in a text block, it will not display all the characters. I'm not sure what I'm doing wrong. I tried several solutions that I found on google, but this did not work for me. Something is missing for me. This is also the first time that I really work with anything other than utf-16. I never had to translate anything.

The application works as follows: downloadstring async (WebClient). Therefore, when this is called, I get a string containing the full RSS feed.

I tried to get bytes and then encoding.convert. But something is missing for me.

Like this example

WebClient RSS = new WebClient(); RSS.Encoding = Encoding.GetEncoding("ISO-8859-1"); RSS.DownloadStringCompleted += new DownloadStringCompletedEventHandler(RSS_DSC); RSS.DownloadStringAsync(new Uri("some rss feed")); public void RSS_DSC(object sender, DownloadStringCompletedEventArgs args) { _xml = XElement.Parse(args.Result); foreach(XElement item in _xml.Elements("channel").Elements("item")) { feeditem.title = item.Element("title").Value; // + all other items } } 

I tried this as well

 private void RSS_ORC(object sender, OpenReadCompletedEventArgs args) { Encoding e = Encoding.GetEncoding("ISO-8859-1"); Stream ez = args.Result; StreamReader rdr = new StreamReader(ez, e); XElement _xml = _xml = XElement.Parse(rdr.ReadToEnd()); feedlist = new List<Code.NewsItem>(); XNamespace dc = "http://purl.org/dc/elements/1.1/"; foreach (XElement item in _xml.Elements("channel").Elements("item")) { Code.NewsItem feeditem = new Code.NewsItem(); feeditem.title = item.Element("title").Value; feeditem.description = item.Element("description").Value; feeditem.pubdate = item.Element("pubDate").Value; feeditem.author = item.Element(dc + "creator").Value; feedlist.Add(feeditem); } listBox1.ItemsSource = feedlist; } 

Although the headers contain characters that are also not displayed. How .. I can make coding work partially. Instead of these characters: a square with a question mark, a question mark, or a square.

Do not misunderstand me. I am new to this. But the solutions that were published on the Internet do not solve it for me.

Please note that I deleted the encoding part because it did not work: / If someone can help me, it will be awesome.

+4
source share
5 answers

You can specify the encoding by specifying the encoding before calling client.DownloadStringAsync :

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

In your sample code, you are not creating an XML document anywhere. Not enough code? You should initialize it with something like:

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

If this helps, you can use:

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

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

+2
source

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

This blog may be useful to you when creating.

+1
source

ISO-8859-1 is most specifically supported in WP7. This is the only one of the ISO-8859- * encodings. I use XmlReader to deserialize RSS feeds, and UTF- * and ISO-8859-1 are the only encodings that this class supports (exceptions from window- * and ISO-8859-2 and above exceptions in XmlReader c'tor).

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

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

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

You may have trouble displaying the upper half of the characters (above 0x80). I had this problem in feeding me (my WP7 application) and used this little hack to fix the situation:

  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 works, of course, only in the WebBrowser control, but this is the only place I've ever seen the wrong display.

Hope this helps, Calum

0
source

This worked for me when I needed to decode rss xml. It is generic enough to support all types of encryption 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(); } 
0
source

All Articles