读取ISO 8859 1编码,使用webclient下载xml流

发布于 2024-12-27 04:08:36 字数 2450 浏览 2 评论 0原文

我正在尝试进行基本的 Web 客户端调用,以获取 WP7 的后期跟踪应用程序的 xml 流。它确实有效,我确实得到了 xml,但问题是因为我住在瑞典,我们有 å ö ä 等特殊字符,对于这些字符,我只得到一个里面有问号的盒子。

我想要获取的 xml 文件如下所示:

<?xml version="1.0" encoding="ISO-8859-1" standalone="yes" ?> 
<pactrack version="2.0" date="Sat Jan 14 18:29:26 CET 2012" size="2125" lang="SE">
    <header>
        <noofparcelentries>1</noofparcelentries> 

...

所以编码是 ISO-8859-1,我想这是我的问题。一直试图在论坛上阅读解决方案,有些人说支持该格式,而有些则不支持: 阅读 iso-8859-1 rss feed C# WP7

我一直在尝试向客户端添加不同的编码,但似乎没有任何帮助,我的 xml 总是缺少特殊符号。然而,有一种奇怪的行为让我有点害怕,如果我添加了错误的跟踪号码,而不是放入特殊字符的数字,我可以突然读取一些特殊字符,我从服务器获得的 xml 是一条错误消息,其中包含追踪号码,见下文,但 xml 定义是相同的。

<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
<pactrack version="2.0" date="Sat Jan 14 18:34:43 CET 2012" size="389" lang="SE" >
<header>
<noofparcelentries>1</noofparcelentries>
<noofuniqueparcels>1</noofuniqueparcels>
</header>
<body>
<parcel id="8538öööåå54248SE">  //I can read this road of xml suddenly
  <customerref></customerref>
  <internalstatus>0</internalstatus>

有人有什么想法吗?我是一个初学者,完全被这个问题迷惑了,所以任何帮助将不胜感激!第一个 xml 和第二个 xml 有什么区别吗?在我看来,也许我看不到嵌套在节点中的特殊章程,这可能是问题所在吗?

    WebClient client = new WebClient();

    public MainPage()
    {
        InitializeComponent();
        client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
        client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
    }

    void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
    {
        if (e.UserState as string == "mobiforge")
        {
            txtStatus.Text = e.BytesReceived.ToString() + "bytes received.";
        }
    }

    public void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        if (e.Error == null && !e.Cancelled)
        {
            MessageBox.Show(e.Result);
        }
    }

    private void btnDownload_Click(object sender, RoutedEventArgs e)
    {          
       client.DownloadStringAsync(new Uri("http://server.logistik.posten.se/servlet/PacTrack?lang=SE&kolliid=85380954248SE"), "posten"); 
    }  

I am trying to make a basic webclient call to get an xml stream for a post tracking app for WP7. It does work and I do get the xml but the problem is as I am living in Sweden we have special characters as å ö ä etc. and for these characters I only get a box with questionmark inside.

The xml file I want to get looks like this:

<?xml version="1.0" encoding="ISO-8859-1" standalone="yes" ?> 
<pactrack version="2.0" date="Sat Jan 14 18:29:26 CET 2012" size="2125" lang="SE">
    <header>
        <noofparcelentries>1</noofparcelentries> 

...

So the encoding is ISO-8859-1 and i guess that is my problem. Been trying to read around here on the forum for a solution and some say that the format is supported while some not:
Reading iso-8859-1 rss feed C# WP7

I been trying to add differnt encodings to the client but nothing seems to help, my xml is missing the special symbols always. There is however a strange behavior that kinda freaks me out, if I add a wrong tracking number, and instead of numbers put in special characters I can suddenly read some of the special characters,the xml I get from the server is an error message containing the tracking number, see below, but the xml definition is the same.

<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
<pactrack version="2.0" date="Sat Jan 14 18:34:43 CET 2012" size="389" lang="SE" >
<header>
<noofparcelentries>1</noofparcelentries>
<noofuniqueparcels>1</noofuniqueparcels>
</header>
<body>
<parcel id="8538öööåå54248SE">  //I can read this road of xml suddenly
  <customerref></customerref>
  <internalstatus>0</internalstatus>

Anyone have any ideas? I am a beginner and totally lost by this problem so any help would be greatly appreciated! Is there any difference in the first xml and the second? It seems to me maybe I cant see special charters that are nested in nodes, can that be the problem?

    WebClient client = new WebClient();

    public MainPage()
    {
        InitializeComponent();
        client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
        client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
    }

    void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
    {
        if (e.UserState as string == "mobiforge")
        {
            txtStatus.Text = e.BytesReceived.ToString() + "bytes received.";
        }
    }

    public void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        if (e.Error == null && !e.Cancelled)
        {
            MessageBox.Show(e.Result);
        }
    }

    private void btnDownload_Click(object sender, RoutedEventArgs e)
    {          
       client.DownloadStringAsync(new Uri("http://server.logistik.posten.se/servlet/PacTrack?lang=SE&kolliid=85380954248SE"), "posten"); 
    }  

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

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

发布评论

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

评论(1

戴着白色围巾的女孩 2025-01-03 04:08:36

根据这个 MSDN 页面,只有这四个Silverlight 支持的编码:

  • utf-8 UTF8编码

  • utf-16 UnicodeEncoding(小尾数)

  • utf-16BE Unicode 编码(大端)

  • utf-16LE UnicodeEncoding(小尾数)

根据链接中的答案之一,用户已设法通过对字符的上半部分进行轻微调整来使其正常工作。我想这对你不起作用?

您可以下载原始字节 (OpenReadAsync) 并对原始字节执行您自己的编码,而不是 DownloadStringAsync此程序可能会帮助您开始这方面的工作。

编辑 - 注意到 MSDN 页面底部的一条评论指出支持 ISO-8859-1。当您尝试此操作时会发生什么:

client.OpenReadAsync(new Uri("http://server.logistik.posten.se/servlet/PacTrack?lang=SE&kolliid=85380954248SE"), "posten");

然后,在回调中,使用编码器读取数据。

var enc = Encoding.GetEncoding("iso-8859-1");
using (var reader = new StreamReader(e.Result, enc))
{
     var result = reader.ReadToEnd();
     Debug.WriteLine(result);
}

According to this MSDN page, only these four encodings are supported in Silverlight:

  • utf-8 UTF8Encoding

  • utf-16 UnicodeEncoding (little-endian)

  • utf-16BE UnicodeEncoding (big-endian)

  • utf-16LE UnicodeEncoding (little-endian)

According to one of the answers in your link, the user has managed to get it to work with a slight tweak to the upper half of the characters. I assume that didn't work for you?

Instead of DownloadStringAsync, you could download the raw bytes (OpenReadAsync) and perform your own encoding on the raw bytes. This program might help you get started with that aspect.

Edit - Noticed a comment at the bottom the MSDN page stating that ISO-8859-1 is supported. What happens when you try this:

client.OpenReadAsync(new Uri("http://server.logistik.posten.se/servlet/PacTrack?lang=SE&kolliid=85380954248SE"), "posten");

Then, in your callback, read the data with the encoder.

var enc = Encoding.GetEncoding("iso-8859-1");
using (var reader = new StreamReader(e.Result, enc))
{
     var result = reader.ReadToEnd();
     Debug.WriteLine(result);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文