I have some data from a database and passed as a response to a web service request.
The Soap formatter happily encodes the 0x12 character (Ascii 18, Device Control 2) as but The response fails on the client with a hexadecimal value of 0x12. It is an invalid character.
I am also willing
>Let the Xml Serialiser handle these strange characters correctly or
>The request fails in the web service
I Searching with google, apart from a) “clean up your input” or b) “change your document structure”, I can’t find much.
a) Not a runner, because some of the data is 20 years
b) is not an option either. In addition to our own front-end, we have the client to directly encode the web service.
Is there anything obvious that I have disappeared? Or is it just a code case around AscII control code?
Thank you
Update
This is actually a problem with XmlSerialiser. The following code will serialize invalid characters into the stream, but will not deserialize them
p>
[Serializable]
public class MyData
{
public string Text {get; set; }
}< br />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 solve the problem of writing xml by explicitly creating an XmlWriter and passing it to Serialise (I will post as my own answer soon), but it still means that I have to send The data was sorted before.
Because these characters are very important, I can’t Strip them, I need to encode them before transmission, and decode them when reading, I am really surprised, there seems to be no existing framework method to do this.
Use DataContractSerializer (used for WCF service by default) instead of XmlSerializer to work
p>
[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 DataContractSerializer(typeof(MyData)) ;
var mem = new MemoryStream();
serializer.WriteObject(mem, myData);
mem.Seek(0, SeekOrigin. Begin);
MyData myData2 = (MyData)serializer.ReadObject(mem);
Console.WriteLine("myData2 {0}", myData2.Text);
}< br />}
First one: Solution
When writing Xml, I can stop it by using XmlWriter, which may be better than the client suffocation. For example
But, it Can’t solve the fundamental problem of sending invalid characters
[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 System.Xml.Serialization.XmlSerializer(typeof(MyData)) ;
var sw = new StringWriter();
XmlWriterSettings settings = new XmlWriterSettings();
using (var writer = XmlWriter.Create(sw))< br /> {
serializer.Serialize(writer, myData); // Exception
// hexadecimal value 0x12, is an invalid character
}
var xmlReader = new StringReader(sw .ToString());
var newUser = (MyData)serializer.Des erialize(xmlReader);
Console.WriteLine("User Name = {0}", newUser);
}
}
Any content below 0x20 (except 0x09, 0x0a, 0x0d, i.e. tab, carrige return and line break) cannot be included in the XML document.
I have some Data from the database and passed as a response to the web service request.
The Soap formatter is happy to encode the 0x12 character (Ascii 18, Device Control 2) as but in the sixteenth The response on the client with the base value of 0x12 failed, it is an invalid character
I am also willing
>Let the Xml Serialiser handle these strange characters correctly or
>The request fails in the web service
I Searching with google, apart from a) “clean up your input” or b) “change your document structure”, I can’t find much.
a) Not a runner, because some of the data is 20 years
b) is not an option either. In addition to our own front-end, we have the client to directly encode the web service.
Is there anything obvious that I have disappeared? Or is it just a code case around AscII control code?
Thank you
Update
This is actually a problem with XmlSerialiser. The following code will serialize invalid characters into the stream, but will not deserialize them
p>
[Serializable]
public class MyData
{
public string Text {get; set; }
}< br />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 solve the problem of writing xml by explicitly creating an XmlWriter and passing it to Serialise (I will post as my own answer soon), but it still means I have to send The data was sorted before.
Since these characters are very important, I cannot strip them, I need to encode them before transmission and decode them when reading, I am really surprised, there seems to be no existing framework method to do this.
Second: Solution
Use DataContractSerializer (used for WCF service by default) instead of XmlSerializer to work
[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 DataContractSerializer(typeof(MyData));
var mem = new MemoryStream( );
serializer.WriteObject(mem, myData);
mem.Seek(0, SeekOrigin.Begin);
MyData myData2 = (MyData)serializer. ReadObject(mem);
Console.WriteLine("myData2 {0}", myData2.Text);
}
}
The first one: Solution
When writing Xml, I can stop it by using XmlWriter, which may be more choking than the client Good. For example
However, it cannot solve the fundamental problem of sending invalid characters
[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 System.Xml.Serialization.XmlSerializer(typeof(MyData));
var sw = new StringWriter();
XmlWriterSettings settings = new XmlWriterSettings();
using (var writer = XmlWriter.Create(sw))
{
serializer.Serialize(writer, myData); // Exception
// hexadecimal value 0x12, is an invalid character
}
var xmlReader = new StringReader(sw.ToString());
var newUser = (MyData)serializer.Deserialize(xmlReader);
Console. WriteLine("User Name = {0}", newUser);
}
}