2011-08-12 40 views
-2

我正在使用XML。 我收到像這樣的XML:從XML設置響應編碼

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

對XXX值爲「NAO OK」,但如何,我從轉換爲「NAO OK」「NØ好嗎?」?

我知道編碼是utf8(1252),但是如何在輸出xml中設置它?

我tryed在請求中設置:

client.Encoding = Encoding.UTF8; 

但不工作。 在此先感謝!

+3

什麼是utf8(1252)肯定是utf8或windows 1252但不是兩者都是 – Mark

+0

爲什麼-2? @calos:是的,我從遠程服務器讀取XML .. –

回答

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(); 
    } 
}