Go xml.Marshal返回无效字符

liu*_*ing 1 xml encoding go

我使用下面的代码生成字符串str的XML编码:

str := string([]byte{0x01})
marshalBytes, _ := xml.Marshal(str)
fmt.Println(string(marshalBytes)) // output: <string>?</string>; ? is [239 191 189] in bytes.
Run Code Online (Sandbox Code Playgroud)

显然, 不等于0x01.

我该如何解决?

Cer*_*món 5

字节[239 191 189]是Unicode替换字符的UTF-8编码.

XML封送程序将字节0x1替换为Unicode替换字符,因为字节0x01不是XML中合法字符.

无法阻止XML封送程序使用替换.