Loading xml with encoding UTF 16 using XDocument

后端 未结 3 1955
情话喂你
情话喂你 2021-02-05 00:13

I am trying to read the xml document using XDocument method . but i am getting an error when xml has


3条回答
  •  难免孤独
    2021-02-05 01:11

    It looks like the file you are trying to read is not encoded as Unicode. You can replicate the behavior by trying to open a file encoded as ANSI with the encoding in the XML file specified as utf-16.

    If you can't ensure that the file is encoded properly, then you can read the file into a stream (letting the StreamReader detect the encoding) and then create the XDocument:

    using (StreamReader sr = new StreamReader(path, true))
    {
        XDocument xdoc = XDocument.Load(sr);
    }
    

提交回复
热议问题