How to read text files with ANSI encoding and non-English letters?

前端 未结 4 996
走了就别回头了
走了就别回头了 2020-12-02 20:21

I have a file that contains non-English chars and was saved in ANSI encoding using a non-English codepage. How can I read this file in C# and see the file content correctly?

4条回答
  •  遥遥无期
    2020-12-02 20:55

    If I remember correctly the XmlDocument.Load(string) method always assumes UTF-8, regardless of the XML encoding. You would have to create a StreamReader with the correct encoding and use that as the parameter.

    xmlDoc.Load(new StreamReader(
                         File.Open("file.xml"), 
                         Encoding.GetEncoding("iso-8859-15"))); 
    

    I just stumbled across KB308061 from Microsoft. There's an interesting passage: Specify the encoding declaration in the XML declaration section of the XML document. For example, the following declaration indicates that the document is in UTF-16 Unicode encoding format:

    
    

    Note that this declaration only specifies the encoding format of an XML document and does not modify or control the actual encoding format of the data.

    Link Source:

    XmlDocument.Load() method fails to decode € (euro)

提交回复
热议问题