读取 iso-8859-1 rss feed C# WP7

发布于 2024-11-14 23:02:49 字数 2053 浏览 3 评论 0原文

我正在尝试读取使用 iso-8859-1 编码的 rss feed。

我可以很好地获取所有元素,问题是当我将其放入文本块时,它不会显示所有字符。我不确定我做错了什么。我尝试了在谷歌上找到的一些解决方案,但这对我不起作用。我一定错过了一些东西..这也是我第一次真正使用 utf-16 以外的任何东西。我以前从未需要转换任何东西。

该应用程序的工作原理如下我下载string async(WebClient)。因此,当调用它时,我会得到一个包含完整 rss 提要的字符串。

我尝试获取字节,然后encoding.convert..但我一定错过了一些东西。

就像这是一个示例,

        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 

                }
    } 

我也尝试过,

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;
    }

尽管标题包含的字符也显示得不好。就像..我可以让编码部分工作。而不是这些字符:带问号的方块、问号或单个方块。

不要误会我的意思,我对此完全是初学者。但网上发布的解决方案并不能解决我的问题。

请注意,我删除了编码部分,因为它不起作用:/ 如果有人能够帮助我那就太棒了。

I'm trying to read a rss feed which uses the iso-8859-1 encoding.

I can get all elements fine, the problem is when I put it in a textblock it will not show all characters. I'm not sure what i'm doing wrong. i've tried a few solutions I found on google but this didn't work for me. I must be missing something.. It's also the first time I really work with anything other than utf-16. I never had to convert anything before.

The app works as follows I downloadstring async(WebClient). So when that is called I get a string containing the complete rss feed.

I have tried getting the bytes, then encoding.convert.. But I must be missing something.

Like this is a sample

        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've tried this aswell

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;
    }

Though titles contain characters that are not displayed well either. Like.. I can get the encoding to partially work. Instead of having these characters: the square with a question mark, a question mark or the singe square.

Don't get me wrong I'm a total beginner on this. But the solutions that has been posted on the web do not solve it for me.

Note that I removed the encoding part because it wasn't working :/
If someone would be able to help me that would be amazing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

扛起拖把扫天下 2024-11-21 23:02:49

您可以通过在调用 client.DownloadStringAsync 之前设置编码来指定编码:

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

在代码示例中,您不会在任何地方创建 XML 文档。是否缺少某些代码?你应该用类似的东西初始化它:

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

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);
心意如水 2024-11-21 23:02:49

如果有帮助,您可以使用:

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

这样每个特殊字符都会被解码,然后您就可以正确显示 myString

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

起风了 2024-11-21 23:02:49

Windows Phone 7 和 Silverlight 不支持其他编码,例如 ISO-8859-1,它们仅支持 ASCII 和 Unicode 编码器。对于其他任何事情,您将需要使用 OpenReadAsync 来获取字节流,然后应用您自己的编码实现。

博客可能对您创建一个博客有所帮助。

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.

梦途 2024-11-21 23:02:49

WP7 绝对支持 ISO-8859-1。它是 ISO-8859-* 编码中唯一的一种。我使用 XmlReader 反序列化 RSS 流,并且 UTF-* 和 ISO-8859-1 是该类支持的唯一编码(windows-* 和 ISO-8859-2 及更高版本在 XmlReader c'tor 中抛出异常)。

尝试使用这样的 XmlReader(不指定编码):

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

XmlReader 将从流中的 xml 声明中获取编码。

显示上半部分字符(0x80 以上)时可能仍然存在问题。我在 feed me(我的 WP7 应用程序)中遇到了这个问题,并使用这个小技巧来解决问题:

    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();
    }

当然,它只能在 WebBrowser 控件中工作,但这是我看到不正确显示的唯一地方。

希望这有帮助,
卡勒姆

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

始于初秋 2024-11-21 23:02:49

当我需要解码 rss xml 时,这对我有用。它足够通用,因此支持 .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();
        }

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();
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文