我正在使用XML。 我收到了这样的XML:
<ajax-response>
<response>
<item>
<number></number>
<xxx>N?o ok</xxx>
<error>null</error>
</item>
</response>
</ajax-response>
xxx值上的是“n ok ok”,但我如何从“N?o ok”转换为“Nãook”?
我知道编码是utf8(1252)但是如何在输出xml中设置它?
我尝试设置请求:
client.Encoding = Encoding.UTF8;
但不起作用。 提前谢谢!
答案 0 :(得分:1)
尝试从代码页1252将编码设置为编码。下面的示例使用简单的服务来提供文件,将编码设置为UTF-8会显示您遇到的相同问题;将其设置为正确的编码工作。
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();
}
}