Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm working with XML. I'm received an XML like this:

<ajax-response>
<response>
<item>
<number></number>
<xxx>N?o ok</xxx>
<error>null</error>
</item>
</response>
</ajax-response>

on xxx value is "n?o ok",but how I convert from "N?o ok" to "N?o ok"?

I know that the codification is utf8(1252) but how set this in output xml?

I tryed setting in request:

client.Encoding = Encoding.UTF8;

but does not works. Thanks in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
269 views
Welcome To Ask or Share your Answers For Others

1 Answer

Try setting the encoding to the encoding from the code page 1252. The example below uses a simple service to serve the file, and setting the encoding to UTF-8 shows the same problem you have; setting it to the correct encoding works.

public class StackOverflow_7044842
{
    const string xml = @"<ajax-response>
<response>
<item>
<number></number>
<xxx>N?o ok</xxx>
<error>null</error>
</item>
</response>
</ajax-response>";

    [ServiceContract]
    public class SimpleService
    {
        [WebGet]
        public Stream GetXml()
        {
            WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
            Encoding encoding = Encoding.GetEncoding(1252);
            return new MemoryStream(encoding.GetBytes(xml));
        }
    }
    public static void Test()
    {
        string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
        WebServiceHost host = new WebServiceHost(typeof(SimpleService), new Uri(baseAddress));
        host.Open();
        Console.WriteLine("Host opened");

        WebClient client = new WebClient();
        client.Encoding = Encoding.GetEncoding(1252);
        string response = client.DownloadString(baseAddress + "/GetXml");
        Console.WriteLine(response);

        Console.Write("Press ENTER to close the host");
        Console.ReadLine();
        host.Close();
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...