低于0x20(除0x09、0x0a、0x0d i.e.tab、汽车回程和线性饲料外)的任何货物不能列入XML文件。
我有一些数据来自数据库,并作为对网络服务要求的回应而通过。
Soap Formatter happily encode 0x12 nature (Ascii 18, Equipment Control 2) as 
,但用户的反应没有hexadecimal Value 0x12, 即无效特性
<rant>
What I find quite frustrating is these are two sides of the same coin, both client and service are .net apps. Why will the soap formatter write bad xml if nothing can read it?</rant>
I d like to either
- Get the Xml Serialiser to handle these odd characters correctly or
- Have the request fail in the Web Service
除了(a) “使你的投入”或(b)“改变你的文件结构”之外,我还ve了这种想法。
a) Isn t a runner as some of this data is +20 years old
b) isn t much of an option either, as other than our own front end, we have clients that code against the Web Service directly.
是否有明显的Im失踪? 或者,它只是AcII控制守则周围的守则?
增 编
Update
This is actually a problem with the XmlSerialiser, the following code will serialise an invalid character to the stream, but will not de-serialise it
[Serializable]
public class MyData
{
public string Text { get; set; }
}
class Program
{
public static void Main(string[] args)
{
var myData = new MyData {Text = "hello "
+ ASCIIEncoding.ASCII.GetString(new byte[] { 0x12 })
+ " world"};
var serializer = new XmlSerializer(typeof(MyData));
var xmlWriter = new StringWriter();
serializer.Serialize(xmlWriter, myData);
var xmlReader = new StringReader(xmlWriter.ToString());
var newData = (MyData)serializer.Deserialize(xmlReader); // Exception
// hexadecimal value 0x12, is an invalid character.
}
}
I can get it to choke writing the xml by explicitly creating an XmlWriter and passing that to Serialise
(I ll post that shortly as my own answer), but that still means I ve to sanatize my data before sending it.
As these characters are significant I can t just strip them, I need to encode them before transmission and decode them when read, and I m really quite surprised that there doesn t appear to be an existing framework method to do this.