0

I am trying to encode my xml text using utf-8 with the code below. For some reason I am getting utf-16 instead of utf-8. Any reason why please?

        StringWriter writer = new StringWriter();
        xdoc.Save(writer);
        writer.Flush();
        string xml = writer.ToString();
        byte[] bytes = Encoding.UTF8.GetBytes(xml);
        System.IO.File.WriteAllBytes(pathDesktop + "\\22CRE002.XPO", bytes);

1 Answer 1

0

StringWriter itself "advertises" (via the TextWriter.Encoding property) an encoding of UTF-16, so the XmlWriter detects that and modifies the XML declaration accordingly. You are actually writing out the data as UTF-8 - it's just that the XML file itself will claim (incorrectly) that it's UTF-16, leading to all kinds of errors.

Your options are:

  • Use a subclass of StringWriter which advertises a different encoding by overriding the Encoding property
  • Just bypass the StringWriter entirely and write straight to the file.

Personally I'd go with the second option:

using (var writer = XmlWriter.Create(Path.Combine(pathDesktop, "22CRE002.XPO"))
{
    xdoc.Save(writer);
}

Why buffer it all in memory first? Note that XmlWriter will already default to UTF-8 if you don't specify an encoding.

5
  • so should I have something like this ? using (var writer = XmlWriter.Create(Path.Combine(pathDesktop, "22CRE002.XPO")) { xdoc.Save(writer); } writer.Flush(); string xml = writer.ToString(); byte[] bytes = Encoding.UTF8.GetBytes(xml); System.IO.File.WriteAllBytes(pathDesktop + "\\22CRE002.XPO", bytes); Please specify thanks a lot Commented Sep 23, 2013 at 14:38
  • ohh sorry for the code :/ can you give me how will the code be emended regarding my code from the question Commented Sep 23, 2013 at 14:39
  • @MarkFenech: No, the code I've given you is the complete code. Don't use the StringWriter at all. If you really need it in a string, please explain why - you haven't really specified your requirements
    – Jon Skeet
    Commented Sep 23, 2013 at 14:41
  • Sure ok sorry than...I have a problem when loading files that I am getting some hidden characters and when I loaded the file with string I didnt get those hidden characters, so kinda problem solved but now im getting this utf 16 problem. Refer to this question - stackoverflow.com/questions/18917838/… Commented Sep 23, 2013 at 14:43
  • @MarkFenech: I've answered the other question. In future, please give appropriate context from the start - if you'd included a link to the previous question before, I'd have just answered that one and added a comment in this one, rather than answering it separately.
    – Jon Skeet
    Commented Sep 23, 2013 at 14:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.