Whaly's World tail -f /var/log/whaly

18Aug/110

Xml deserialization error with invalid character

Recently I had a strange error, I serialized a complex object and during the deserialization process I got :
"Error : System.Xml.XmlException: '.', hexadecimal value 0x00, is an invalid character. Line X, position Y."
It appears that we had a "\0" inside a string, something like "Hello\0World" ! and during a classic serialization, the character was encoded in "�"

With the help of Google I have found this post (2007) with a way to have an happy deserialization :-)

You can use XmlTextReader instead of XmlReader, but with more research I have found that you can still use XmlReader with XmlReaderSettings and CheckCharacters set to false.

Here is an example :

    public class MyObject
    {
        public string MyString { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {

            XmlSerializerFactory fact = new XmlSerializerFactory();
            XmlSerializer ser = fact.CreateSerializer(typeof(MyObject));

            MyObject obj0 = new MyObject();
            obj0.MyString = "Hello\0World";

            // Serialize the object
            StringWriter sw = new StringWriter();
            ser.Serialize(sw, obj0);
            string xml = sw.ToString();
            // We can check that in the xml a \0 is transformed in �
            Console.WriteLine(xml);

            // Classic use of XmlReader.Create
            StringReader sr1 = new StringReader(xml);
            XmlReader xr1 = XmlTextReader.Create(sr1); // xr1's type is XmlTextReaderImpl
            try
            {
                MyObject obj1 = (MyObject)ser.Deserialize(xr1);
                Console.WriteLine("XmlReader [CheckCharacters({0})] : Success : {1}", xr1.Settings.CheckCharacters, obj1.MyString);
                Console.WriteLine(obj1.MyString);
            }
            catch (Exception e)
            {
                Console.WriteLine("XmlReader [CheckCharacters({0})] : Error : {1}", xr1.Settings.CheckCharacters, e.InnerException);
            }

            // Using an XmlTextReader
            StringReader sr2 = new StringReader(xml);
            XmlTextReader xr2 = new XmlTextReader(sr2);
            // xr2.Settings is null
            MyObject obj2 = (MyObject)ser.Deserialize(xr2);
            Console.WriteLine("XmlTextReader : Success : {0}", obj2.MyString);

            // Using XmlReader with the good XmlReaderSettings
            StringReader sr3 = new StringReader(xml);
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.CheckCharacters = false; // default value is true;
            XmlReader xr3 = XmlTextReader.Create(sr3, settings); // xr3.Settings.CheckCharacters is a read only and xr3's type is XmlTextReaderImpl
            MyObject obj3 = (MyObject)ser.Deserialize(xr3);
            Console.WriteLine("XmlReader [CheckCharacters({0})] : Success : {1}", xr3.Settings.CheckCharacters, obj3.MyString);
        }
    }
Tagged as: , No Comments
10Mar/100

Error 415 and WCF Compatibility between .Net 2.0 and 3.5

If you try to call a 3.5 WCF Service with the framework 2.0 and WCF 3.0, you will have an error 415 :

Cannot process the message because the content type 'text/xml; charset=utf-8' was not the expected type 'application/soap+xml; charset=utf-8'

'text/xml' is the content type for SOAP 1.1 while 'application/soap+xml' is the content type for SOAP 1.2 and your client WCF 3.0 use SOAP 1.1 while the service with WCF 3.5 use SOAP 1.2.

Here is the solution that worked for me. If you have access to the service config, you just have to change the binding to use SOAP 1.1 :

Add a new custom binding in the 'bindings' section

      <customBinding>
        <binding name="Soap11HttpBinding">
          <textMessageEncoding messageVersion="Soap11" />
          <httpTransport />
        </binding>
      </customBinding>

and don't forget to update your endpoint with the new binding :

    <endpoint ...
        binding="customBinding"
        bindingConfiguration="Soap11HttpBinding">
        ...
    </endpoint>
28Jan/100

How to remove Diacritics in .net C#

If you need to remove diacritics from a string you can :

  • [Bad] do a lot's of replace like :-(
inputString = inputString.Replace('À', 'A');

repeat for ÀÁÂÃÄâãäàáÈÉÊËêëèéÌÍÎÏîïìíÒÓÔÖôõöòóÙÚÛÜûüùúÝýÑñç and maybe miss some exotic chars or the Õ in this list !

  • [Good] or you can use this function :-)
	public static string RemoveDiacritics(string inputString)
	{
		//!\\ Warning 'œ' will be replaced with a 'o' not an 'oe'
		String normalizedString = inputString.Normalize(NormalizationForm.FormD);
		StringBuilder stringBuilder = new StringBuilder();
		for (int i = 0; i < normalizedString.Length; i++)
		{
			Char c = normalizedString[i];
			if (System.Globalization.CharUnicodeInfo.GetUnicodeCategory(c) != System.Globalization.UnicodeCategory.NonSpacingMark)
				stringBuilder.Append(c);
		}
		return stringBuilder.ToString();
	}
9Oct/090

WCF WSDL SOAP binding: Rpc or Document, Encoded or Literal

Yeah, my new WebService must be Rpc/Encoded. What ????

Hopefully this page from Ibm explain the different style/use in SOAP message.

A WSDL document describes a Web service. A WSDL binding describes how the service is bound to a messaging protocol, particularly the SOAP messaging protocol. A WSDL SOAP binding can be either a Remote Procedure Call (RPC) style binding or a document style binding. A SOAP binding can also have an encoded use or a literal use.

Ok now I know that four style/use models:
RPC/Encoded, RPC/literal, Document/encoded or Document/literal

But how do I do that in WCF C# ?? It's very easy, just use the [XmlSerializerFormat] attribute.

Here is a very very simple test to check the different result in the soap message:

Interface of the Wcf service used :

using System.ServiceModel;

namespace WcfService1
{
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        [XmlSerializerFormat(Style = OperationFormatStyle.Rpc, Use = OperationFormatUse.Encoded)]
        string TestRpcEncoded(int value);

        [OperationContract]
        [XmlSerializerFormat(Style = OperationFormatStyle.Rpc, Use = OperationFormatUse.Literal)]
        string TestRpcLiteral(int value);

        //[OperationContract]
        //[XmlSerializerFormat(Style = OperationFormatStyle.Document, Use = OperationFormatUse.Encoded)]
        //string TestDocumentEncoded(int value);

        [OperationContract]
        [XmlSerializerFormat(Style = OperationFormatStyle.Document, Use = OperationFormatUse.Literal)]
        string TestDocumentLiteral(int value);
    }
}

Result captured on the network with fiddler:

For clarity, I have removed:

  • s:Envelope tag
  • xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"
  • xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  • xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  • xmlns="http://tempuri.org/"

RPC / Encoded call

<!-- Request -->
<s:Body s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  <q1:TestRpcEncoded>
    <value xsi:type="xsd:int">42</value>
  </q1:TestRpcEncoded>
</s:Body>

<!-- Response -->
<s:Body s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  <q1:TestRpcEncodedResponse>
    <TestRpcEncodedResult xsi:type="xsd:string">You entered: 42</TestRpcEncodedResult>
  </q1:TestRpcEncodedResponse>
</s:Body>

RPC / Literal call

<!-- Request -->
<s:Body>
  <TestRpcLiteral>
    <value xmlns="">42</value>
  </TestRpcLiteral>
</s:Body>

<!-- Response -->
<s:Body>
  <TestRpcLiteralResponse>
    <TestRpcLiteralResult xmlns="">You entered: 42</TestRpcLiteralResult>
  </TestRpcLiteralResponse>
</s:Body>

Document / Encoded call
If you add the TestDocumentEncoded you will get an InvalidOperationException.
This combination is not supported.

Document / Literal call

<!-- Request -->
<s:Body></code>
  <TestDocumentLiteral><value>42</value></TestDocumentLiteral>
</s:Body>

<!-- Response -->
<s:Body>
  <TestDocumentLiteralResponse>
    <TestDocumentLiteralResult>You entered: 42</TestDocumentLiteralResult>
  </TestDocumentLiteralResponse>
</s:Body>
9Aug/092

The time separator

DateTime.Now.ToString("HH:mm:ss");

is a common mystake if you want to transmit to someone or to be sure that you have something like "HH:mm:ss" because the character (:) is not just a colon, it's a time separator wich depend on the locale.

Here is a list of culture where the (:) change into a dot (.)
it-IT : Italian (Italy)
fo-FO : Faroese (Faroe Islands)
bn-BD : Bengali (Bangladesh)
ml-IN : Malayalam (India)
bn-IN : Bengali (India)

To workaround you can use CultureInfo.InvariantCulture or escape the colon character.

... and same story for the date separator "/" but this one is at this time the same for every culture.

Tagged as: , , 2 Comments
20Jul/090

Changing the subtitle language in Monkey Island Special Edition

The first time you start Monkey Island SE, you can choose the subtitle language, but after that you can't do it anymore with the game interface.

You can follow the instruction on the steam support page or you can use my little tool and if you don't trust me the source code is available.

Have fun !
Whaly



16Jul/092

XmlWriter and UTF-8 encoding without signature

I used this code to serialize some objects in Xml :

XmlWriter writer = new XmlTextWriter(stream, Encoding.UTF8);

But the output contains an UTF header, the Byte Order Mark (BOM). The use of the header/signature is usually for xml file, if you want to use the ouput in an HttpResponse, you don't need the signature. (some parser can cause a parsing error in java, like org.xml.sax.SAXException).

Here is the change to remove the BOM :

XmlWriter writer = new XmlTextWriter(stream, new UTF8Encoding(false));
Tagged as: , , , 2 Comments