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

后端 未结 2 778
灰色年华
灰色年华 2020-12-10 10:25

I am writing an XML validator with XSD.

Below is what I did, but when the validator reached the line while (list.Read()) it gives me the error

相关标签:
2条回答
  • 2020-12-10 10:49

    The reality of your file's encoding appears to conflict with that specified by your XML declaration. If your file actually uses one-byte characters, declaring encoding="utf-16" won't change it to use two-byte characters, for example.

    Try removing the conflicting encoding from the XML declaration. Replace

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

    with

    <?xml version="1.0"?>
    

    You may also be able to load the file into a string as a work-around using LoadXML().

    0 讨论(0)
  • 2020-12-10 10:57

    If you are not able to change the xml file encoding as

    <?xml version="1.0"?>
    

    Alternatively, you can read the xml content directly as raw xml instead of loading it with xml path.

    XmlReader.Create(new StringReader(File.ReadAllText(fileName)));
    

    If you use XmlDocument;

    var xmlDoc = new XmlDocument();
    xmlDoc.LoadXml(File.ReadAllText(filePath));
    
    0 讨论(0)
提交回复
热议问题