-1

I get the following error (which I see on other SO posts):

There is no Unicode byte order mark. Cannot switch to Unicode.

I see on other SO posts that users swith:

<?xml version="1.0" encoding="utf-16"?>

to:

<?xml version="1.0" encoding="utf-8"?>

I cannot update all my importing XMLs on the fly to switch this so I want to know how to do it programmatically. I have the following code where the

    private static string ParseXML(Stream xmlSteam)
    {
        using (var xmlReader = XmlReader.Create(xmlSteam))
        {
            // Errors out here with the unicode exception being caught
            while (xmlReader.Read())

I am trying to fix it so it will read and parse the XML.

cdub
  • 24,555
  • 57
  • 174
  • 303

1 Answers1

0

If your XML is encoded in UTF-8 but says it is encoded in UTF-16 then it's bad data, and the best thing to do with bad data is to find out where the corruption occurred and fix it at source.

If you can't fix it at source, but you know exactly what's wrong, then read the data into a MemoryStream and adjust the content before passing it to the XML parser.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164