4
votes

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"); 
    }  
1

1 Answers

4
votes

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