Hey, hey.

Today, I discovered that the StringReaders/Writers in .Net only support UTF-16 encoding, which may lead to some breakage if you’re using it in combination with the XMLSerializer object.

If you really need to maintain UTF-8 compatibility, you should use a MemoryStream. This is actually just as simple as using a StringReader:

  MemoryStream m = new MemoryStream();
  try
  {
    XmlSerializer formatter = 
        new XmlSerializer(typeof(TMakeUpYourMind));
    formatter.Serialize(m, this);
    m.Flush();
    m.Seek(0, 0);
    StreamReader rd = new StreamReader(m);
    try
    {
      res = rd.ReadToEnd();
    }
    finally
    {
      rd.Dispose();
    }
  }
  finally
  {
    m.Dispose();
  }

Don’t forget to ‘rewind’ the stream to position 0.

I’m not sure why this isn’t mentioned in the SDK. Also note to the Visual Studio Express developers: please make this weekly ‘registration of Visual Studio Express’ on my computer stop. I’m sure it’s a bug because everytime I register I get the same e-mail in with the same deals. I’ve already got 12 of them. Please.