Difference between Encoding.UTF8.GetBytes and UTF8

2019-06-16 03:26发布

问题:

Can someone please explain me what is the difference bet. Encoding.UTF8.GetBytes and UTF8Encoding.Default.GetBytes? Actually I am trying to convert a XML string into a stream object and what happens now is whenever I use this line:

  MemoryStream stream = new MemoryStream(UTF8Encoding.Default.GetBytes(xml));

it gives me an error "System.Xml.XmlException: Invalid character in the given encoding"

but when I use this line it works fine:

  **MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(xml));**

Even though it seems to be UTF8 encoding in both cases how one works and the other does not?

回答1:

There is no UTF8Encoding.Default property. When you write this, you're actually returning the base class static property, Encoding.Default, which is not UTF8 (it's the system's default ANSI code-page encoding).

As such, the two will return very different results - since UTF8Encoding.Default is actually Encoding.Default, you will return the same thing as if you use ASCIIEncoding.Default or any of the other System.Text.Encoding subclasses.

The proper way to use UTF8Encoding is with an instance you create, such as:

MemoryStream stream = new MemoryStream((new UTF8Encoding()).GetBytes(xml));

The above should provide the same results as:

MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(xml));