Å Ö characters are not displayed in my DDL, how can I tell restclient to use a specific encoding?

Before I start here, the problem. It should be like this:

Björn Nilsson, instead displaying strange special characters, all values ​​that have the characters Å, Ä and Ö will be like that.

enter image description here

I populate my DDL with values ​​from an API in XML format, which has all the values, and we also use Linq2Rest for this.

What does the process look like

 private readonly RestContext<ADConsultants> restContext;

public ConsultantContext(Uri uri, Format format)
{
    restContext = new RestContext<ADConsultants>(GetRestClient(uri, format), GetSerializerFactory(format));
}
public enum Format
{
    Pox,
    Json
}

private static readonly IEnumerable<Type> knownTypes = new[] {typeof (ADConsultants)};

public static IRestClient GetRestClient(Uri uri, Format format)
{
    switch (format)
    {
        case Format.Pox:
            return new XmlRestClient(uri);
        case Format.Json:
            return new JsonRestClient(uri);
        default:
            throw new NotImplementedException();
    }
}
private static ISerializerFactory GetSerializerFactory(Format format)
{
    switch (format)
    {
        case Format.Pox:
            return new XmlSerializerFactory(knownTypes);
        case Format.Json:
            return new JsonNetSerializerFactory();
        default:
            throw new NotImplementedException();
    }

}
public IQueryable<ADConsultants> Consultant
{
    get { return restContext.Query; }
}

}

This is my JsonNetSerializerFactory class:

public class JsonNetSerializerFactory :ISerializerFactory 
{
    public ISerializer<T> Create<T>()
    {
        return new JsonNetSerializer<T>();
    }
    public class JsonNetSerializer<T> : ISerializer<T>
    {
        public T Deserialize(string input)
        {
            return JsonConvert.DeserializeObject<T>(input);
        }

        public IList<T> DeserializeList(string input)
        {
            return JsonConvert.DeserializeObject<IList<T>>(input);
        }
    }
}

And this is inside my controller:

var consultants = new ConsultantContext(
        new Uri("http://adress:port/api/consultants"),
                ConsultantContext.Format.Json)
                    .Consultant
                    .Where(x => x.Office == "Örebro")  
                    .OrderBy(x => x.DisplayName)  
                    .ToList() 
                    .Select(x => new
                    {
                        name = x.DisplayName
                    });

I did a test by doing the following:

name = "åäö"

and it worked fine, the ddl values ​​were "åäö"

Any help on how to fix such characters is appreciated. Å ... works fine as values ​​in my DDL.

HTTP- - utf-8, html-. XML, . ?

!

+5
2

/ .

: , , iso-8859-1 iso-8859-15. () "UTF-8". , , UTF-8, UTF-8 (Ä, Ü, Ö ..). UTF-8 .

1- (Re) (, "Björn Nilsson" ) (iso-8859-1/iso-8859-15) .

2- UTF-8.

:

using System;
using System.Collections.Generic;
using System.Text;

    namespace csharp.util.charset
    {
        public class SysUtil
        {
            /// <summary>
            /// Convert a string from one charset to another charset
            /// </summary>
            /// <param name="strText">source string</param>
            /// <param name="strSrcEncoding">original encoding name</param>
            /// <param name="strDestEncoding">dest encoding name</param>
            /// <returns></returns>
            public static String StringEncodingConvert(String strText, String strSrcEncoding, String strDestEncoding)
            {
                System.Text.Encoding srcEnc = System.Text.Encoding.GetEncoding(strSrcEncoding);
                System.Text.Encoding destEnc = System.Text.Encoding.GetEncoding(strDestEncoding);
                byte[] bData=srcEnc.GetBytes(strText);
                byte[] bResult = System.Text.Encoding.Convert(srcEnc, destEnc, bData);
                return destEnc.GetString(bResult);
            }

        }
    }

:

(JSON-, XML, ) / ,

String content = "Björn Nilsson";
SysUtil.StringEncodingConvert(content, "ISO-8859-1","UTF-8");

( , ):

public class JsonNetSerializerFactory :ISerializerFactory 
{
    public ISerializer<T> Create<T>()
    {
        return new JsonNetSerializer<T>();
    }
    public class JsonNetSerializer<T> : ISerializer<T>
    {
        public T Deserialize(string input, String fromCharset, String toCharset)

        {
           String changedString = SysUtil.StringEncodingConvert(input, fromCharset,toCharset);

            return JsonConvert.DeserializeObject<T>(changedString  );
        }

        public IList<T> DeserializeList(string input, String fromCharset, String toCharset)
        {
         String changedString =  SysUtil.StringEncodingConvert(input, fromCharset,toCharset);

            return JsonConvert.DeserializeObject<IList<T>>(changedString);
        }
    }
}

JsonNetSerializerFactory  

,

XmlSerializerFactory  

HTML-

<meta charset="utf-8"> <!--HTML 5 -->

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <!-- if HTML version < 5-->
+2

, .

<meta> ?

, , , UTF-8, HTML5,

<meta charset="utf-8">

HTML

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

iso-8859-1 charset.

+1

All Articles