0

I am getting data from a web service end point and place it into a list in a for each loop. The service gets it's data from a Wordpress website.

 var list = new ItemList
      (
         (string)data.id.ToString(),
          (string)data.name,

          (string)subcategory

     );

I then print this on the XAML page. The code works fine in that it successfully gets the data from the service and prints it on the page of my windows 8 app.

However in (string)data.name,, which is the name of the items, if the name contains a "&" it shows up in the app as $#038;. Also if a item name contains a "'", apostrophe s, it shows up as ’.

EG. D & G, shows up as D $#038; G

The "&" and "'" show up as these weird symbols.

How do I get rid of these and fix it so that they render correctly in the app.

Tester
  • 2,887
  • 10
  • 30
  • 60
  • What happens between (string)data.name and the UI layer? If you debug, does data.name already contain these "weird symbols" (actually just HTML encoded symbols), or does data.name contain only the original & or ' and then the UI ends up showing the symbols? – Dmitriy Khaykin Feb 12 '14 at 18:52
  • @DavidKhaykin The names of items from the web service source shows up fine without these characters. When I use i in the app UI or even when I call the service url in windows powershell to view to results, the name shows up with weird symbols as well. – Tester Feb 12 '14 at 18:58
  • @Tester When you say "web service source", are you refering to "web service response"? Or what? – Matías Fidemraizer Feb 12 '14 at 19:00
  • Well the web service was built to get it's data from a website. This is the source. The names show fine in the website. But when I try to access the names through end points (Eg. www.test.com/items/), whether in my app ui or in power shell, the response show the names with these symbols – Tester Feb 12 '14 at 19:02
  • @Tester Are you talking about a *crawler*? – Matías Fidemraizer Feb 12 '14 at 19:04
  • Is the website wordpress by any chance? In some googling I seem to find that particular character encoding being related to wordpress a lot. Just curious. – Dmitriy Khaykin Feb 12 '14 at 19:05
  • @DavidKhaykin Yes it is Wordpress – Tester Feb 12 '14 at 19:08
  • Ok got it. So you're calling a WebService provided by WordPress, hence you are getting string already encoded from webservice and into your app. It was hard to understand the question the way it was worded. – Dmitriy Khaykin Feb 12 '14 at 19:12
  • @DavidKhaykin Yes that's it – Tester Feb 12 '14 at 19:16
  • @Tester for your future questions it makes it a lot easier for people to answer when you include these kinds of details in the question. – Dmitriy Khaykin Feb 12 '14 at 19:17
  • @Tester Please provide a string literal sample of the text you're receiving so we can guide you better... Thanks – Matías Fidemraizer Feb 12 '14 at 19:17
  • possible duplicate of [Convert character entities to their unicode equivalents](http://stackoverflow.com/questions/5783817/convert-character-entities-to-their-unicode-equivalents) – Matías Fidemraizer Feb 12 '14 at 19:19
  • @MatíasFidemraizer Please see the example above – Tester Feb 12 '14 at 19:22
  • @Tester I asked you for a string literal! I'm very sure that anyone trying to help you doesn't expect explained examples... Just throw in your question a FULL string literal. – Matías Fidemraizer Feb 12 '14 at 19:23

3 Answers3

2

I'm going to take the risk of giving you a wrong hint, because I guess you're talking about a Windows 8 Store App (XAML), thus you don't have access to every class on .NET, but...

What about decoding HTML entities?

Check this HttpUtility method: HtmlUtility.HtmlDecode.

Check WebUtility.HtmlDecode, which is on System.dll, thus available for Windows 8 Store Apps.

You'll need to add a reference to System.Web on your Visual Studio project.

Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206
  • This doesn't work for these characters. `HtmlDecode` will decode `&` but not `$#038;`. – Dmitriy Khaykin Feb 12 '14 at 18:46
  • @DavidKhaykin I'm going to try myself in a console app... if you're right, I'll delete that answer. Should I guess you downvoted the answer...? Remember I said "I'm going to take the risk..." – Matías Fidemraizer Feb 12 '14 at 18:48
  • @DavidKhaykin By the way... `$#038;` isn't the text OP is trying to decode. It's what the app shows in the UI, am I wrong? – Matías Fidemraizer Feb 12 '14 at 18:49
  • You guess correctly. I don't downvote often, but when I do, it's because the answer is wrong. – Dmitriy Khaykin Feb 12 '14 at 18:49
  • Correct, but that implies that something at the receiving end in his app is doing the encoding to result in that character string. – Dmitriy Khaykin Feb 12 '14 at 18:50
  • @DavidKhaykin I believe you guessed a lot of things. Check OP's question: " if the name contains a "&" it shows up in the app as $#038;". **It shows in the app**. This implies **that the Web service client isn't decoding the entities**. – Matías Fidemraizer Feb 12 '14 at 18:51
  • Other way around -- if the web service result is not encodd and the UI is then something is encoding it before it gets to the UI. I am not guessing but that is how I interpreted it. I asked OP to clarify in a comment. – Dmitriy Khaykin Feb 12 '14 at 18:53
  • Either way, HtmlDecode does not actually decode that string, and OP wants to get rid of the symbols, hence HtmlDecode not being the answer. No need to argue, I removed the downvote. – Dmitriy Khaykin Feb 12 '14 at 18:54
  • @DavidKhaykin I'm trying to "try" the solution... :) I told you if it's not working, I'll remove it. – Matías Fidemraizer Feb 12 '14 at 18:54
  • @DavidKhaykin `string a = System.Web.HttpUtility.HtmlDecode("&");` has the expected result. You said " HtmlDecode does not actually decode that string"... Why not? O_o – Matías Fidemraizer Feb 12 '14 at 18:58
  • @DavidKhaykin I did in a console app. By the way, I really doubt host environment would modify the result...! – Matías Fidemraizer Feb 12 '14 at 19:02
  • Ok. I surrender my internet card. I had "$#038;" and kept misreading it. Nothing to see here, carry on :) – Dmitriy Khaykin Feb 12 '14 at 19:02
  • @DavidKhaykin Hahaha, no problem! BTW check that OP isn't explaining the problem very well :/ "web service source" what? – Matías Fidemraizer Feb 12 '14 at 19:03
  • @MatíasFidemraizer I tried `(string)System.Net.WebUtility.HtmlDecode(data.title)`, but the synbols stayed, didn't change anything, and ` (string)System.Web.HttpUtility.HtmlDecode(data.title),` but this throws a build error. "web" namespace not recognized – Tester Feb 12 '14 at 19:05
  • @Tester, read Matias' answer again, he says you'll need to add reference to that assembly in your project. – Dmitriy Khaykin Feb 12 '14 at 19:11
  • @Tester What you're calling "symbols" are HTML entities... Try to call things by its name!!! And about "didn't change anything", can you provide the exact string you're trying to decode? – Matías Fidemraizer Feb 12 '14 at 19:16
  • Also in the question it says data.name, now it's data.title?? – Dmitriy Khaykin Feb 12 '14 at 19:16
  • @MatíasFidemraizer DO you know of any issues with adding the reference system.web to a windows 8 application in vs 2012? I tried, but can't seem to add it to try your answer. It should be a simple ref add, but I can't find it. I found some articles taht said to change the app Target Framework, but in my properties page, the Target Framework section is greyed out. – Tester Feb 12 '14 at 19:50
  • The top of the dialog box for adding a ref has the text "Targeting:.Net for Windows store apps" – Tester Feb 12 '14 at 19:54
  • @MatíasFidemraizer Found this http://rarcher.azurewebsites.net/Post/PostContent/23, so I cant use this answer – Tester Feb 12 '14 at 20:01
  • @Tester Can you try the whole `WebUtility.HtmlDecode` which is an equivalent to `HttpUtility`, but in System.Net? Can you add System.Net? – Matías Fidemraizer Feb 12 '14 at 20:09
  • @MatíasFidemraizer I tried `(string)WebUtility.HtmlDecode(data.name)`, but I get the error "Best overloaded method match for `WebUtility.HtmlDecode(string)` has some invalid arguments ". – Tester Feb 12 '14 at 20:19
  • Does this change the data type or anything? – Tester Feb 12 '14 at 20:22
  • @Tester What? No. It decodes a string, so it returns a string. Now you'll need to read the linked MSDN article. Please do you homework!! :) – Matías Fidemraizer Feb 12 '14 at 20:46
  • Actually i figured it out. I needed to assign the data.name to a string variable first, then user htmldecode on the string variable. – Tester Feb 12 '14 at 20:53
0

You can use WebUtility.HtmlDecode Method (String)

Or you can use if you don't want to add additional libraries.

public string Decode(string text)
{
    var replacements = new Dictionary<string, char> {
      { "&#8217;", ''' },
      // ...etc
    }

    var sb = new StringBuilder( text );

    foreach( var c in replacements.Keys ) {
      sb.Replace( c.ToString(), replacements[c] );
    }

    return sb.ToString();
}
Daniil Grankin
  • 3,841
  • 2
  • 29
  • 39
0

It looks like the service is returning XML escaped entities. &#038; means a character with a code of (decimal) 38 (which is &). &#8217; is similar and means a code of 8217 (which is ).

You can decode these using System.Web.HttpUtility.HtmlDecode(inputString), but that requires a reference to System.Web. If you don't want to or cannot reference that, you can try something like this:

var xml = new XmlDocument();
xml.LoadXml("<x>" + inputString + "</x>");
var output = xml.InnerText;

Given Testing &#8217;stuff&quot; &#038; things, it will return Testing ’stuff" & things.

I'd go with HtmlDecode() if you can, but absolutely try and avoid rolling your own decoder unless you have no other choice.

Nick
  • 3,722
  • 2
  • 26
  • 17
  • I tried this but I get build errors. I an't seem to reference system.web. – Tester Feb 12 '14 at 19:10
  • You probably just need to include a reference to System.Web in Visual Studio. Right-click on References, choose Add Reference. Under Assemblies you should be able to find System.Web. – Nick Feb 12 '14 at 19:49
  • I just commented on this below. I can't seem to find it. My app is a windows 8 store app. When I go to add a ref, under assemblies, it says at the top, Targeting:.Net for Windows store apps – Tester Feb 12 '14 at 19:54