我想將一些數據發佈到我沒有的WCF服務。我使用XmlSerializer來格式化消息,這會導致包含單引號的數據出現問題。WCF服務不喜歡單引號
有這樣
...
<FirstName>D'Arcy</FirstName>
...
原因服務的節點拋出 「400錯誤的請求」 異常。如果我手動構建這樣的提琴手請求
...
<FirstName>D"Arcy</FirstName>
...
然後它工作得很好。
我很困惑,爲什麼這將是必要的。各種在線XML驗證器聲稱我的原始XML是有效的,但WCF不喜歡它?任何方式來解決/解決方法?
這裏是我的代碼,以防萬一:
static XmlSerializer RequestSerializer = new XmlSerializer(typeof(Message));
static XmlSerializer ResponseSerializer = new XmlSerializer(typeof(int), new XmlRootAttribute("int") { Namespace = "http://schemas.microsoft.com/2003/10/Serialization/" });
static XmlWriterSettings WriterSettings = new XmlWriterSettings { OmitXmlDeclaration = true, CloseOutput = false, Encoding = new UTF8Encoding(false) };
private static int PostData(Message msg)
{
var request = (HttpWebRequest)WebRequest.Create("https://...");
request.ContentType = "text/xml;charset=UTF-8";
request.Method = "POST";
using (var writer = XmlWriter.Create(request.GetRequestStream(), WriterSettings))
RequestSerializer.Serialize(writer, msg);
using (var response = (HttpWebResponse)request.GetResponse())
using (var responseStream = response.GetResponseStream())
{
return (int)ResponseSerializer.Deserialize(responseStream);
}
}
[XmlRoot("Order", Namespace = "http://...")]
public class Message
{
public string City;
public string Coupon;
public DateTime CreateDate;
public string Email;
public string FirstName;
public string Language;
public string LastName;
public int OrderID;
public string PostalCode;
public string ProductID;
public string Province;
public string StreetAddress1;
public string StreetAddress2;
}
你確定單引號轉義爲",而不是'? –
@WiktorZychla嗯,你是對的,當然。 –
@WiktorZychla奇怪的是使用'''產生同樣的問題作爲未編碼的''' –