Read ISO 8859 encoding 1, download xml stream using webclient

I am trying to make a main webclient call to get the xml stream for a message tracking application for WP7. It works, and I get xml, but the problem is, since I live in Sweden, we have special characters like å ö ä, etc., And for these characters I only get a box with a question mark 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 think this is my problem. I tried to read here on the forum for a solution, and some say that the format is supported, and some do not: Read iso-8859-1 C # WP7 RSS feed

I tried to add excellent encodings to the client, but nothing helps, my xml always skips special characters. However, there is a strange behavior that causes me if I add the wrong tracking number, and instead of numbers placed in special characters, I can suddenly read some special characters, the xml that 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> 

Does anyone have any ideas? I am new and completely lost this problem, so any help would be greatly appreciated! Is there a difference in the first xml and the second? It seems to me, maybe I don’t see special charters that are nested in the nodes, maybe 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"); } 
+4
source share
1 answer

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

  • 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 managed to get him to work with a small setting for the upper half of the characters. I suppose that didn't work for you?

Instead of DownloadStringAsync you can download raw bytes ( OpenReadAsync ) and perform your own encoding on raw bytes. This program can help you get started with this aspect.

Change We noticed a comment at the bottom of the MSDN page with the support message ISO-8859-1 . What happens when you try to do this:

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

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

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

Source: https://habr.com/ru/post/1391009/


All Articles