WCF service does not like single quotes

I am trying to send some data to a WCF service that I do not own. I use XmlSerializer to format a message that creates problems with single-quoted data.

Having a node like this

... <FirstName>D'Arcy</FirstName> ... 

Calls the service to throw a 400 bad request exception. If I manually create a request in Fiddler, like this

 ... <FirstName>D&quot;Arcy</FirstName> ... 

then it works great.

I am confused why this is necessary. Various online XML validators claim my original XML is valid, but doesn't like WCF? Any way to fix / workaround?


Here is my code just in case:

 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; } 
+4
source share
1 answer

It turns out that this WCF service inserts data into its database using a direct query (not parameterized) and does not avoid single quotes. This was really confusing since the HTTP 400 status for me implies that it was a problem with deserialization or something similar within the framework.
Sorry to bother everyone :(

+3
source

All Articles