Change the encoding to UTF-8 on a stream (MemoryMappedViewStream)

江枫思渺然 提交于 2019-12-06 03:55:37

You could use the StreamReader class to set the encoding:

static void Main(string[] args)
{
  using (var file = MemoryMappedFile.CreateFromFile(@"d:\temp\temp.xml", FileMode.Open,  "MyMemMapFile"))
  {
     using (MemoryMappedViewStream stream = file.CreateViewStream())
    {
        Read(stream);
    }
   }
}

static void Read(Stream stream)
{
  using (XmlReader reader = XmlReader.Create(new StreamReader(stream, Encoding.UTF8)))
  {
     reader.MoveToContent();

    while (reader.Read())
    {
    }
 }
}

Hope, this helps.

On MSDN you get the following.

"The XmlReader scans the first bytes of the stream looking for a byte order mark or other sign of encoding"

Does your xml file specify an encoding?

<?xml version="1.0" encoding="UTF-8"?>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!