我有以下代码来创建XML文件。为什么不对传入的字符串进行UTF-8转换?
using System.IO;
using System.Text;
using System.Xml;
namespace TestXmlWriter
{
class Program
{
private static readonly Encoding utf8NoBOM = new UTF8Encoding(false);
static void Main(string[] args)
{
Stream stream = new FileStream("c:\\temp\\test.xml", FileMode.Create, FileAccess.ReadWrite);
XmlWriter xmlWriter = new XmlTextWriter(stream, utf8NoBOM);
xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement("bear");
xmlWriter.WriteAttributeString("version", "CONSULTOR_TÉCNICO");
xmlWriter.WriteEndElement();
xmlWriter.WriteEndDocument();
xmlWriter.Flush();
stream.Close();
}
}
}
输出:
<?xml version="1.0" encoding="utf-8"?><bear version="CONSULTOR_TÉCNICO" />