Loading xml with encoding UTF 16 using XDocument

后端 未结 3 1944
情话喂你
情话喂你 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:00

    I tried , and found another way of doing it !!

    XDocument xdoc = XDocument.Parse(System.IO.File.ReadAllLines(path));
    
    0 讨论(0)
  • 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);
    }
    
    0 讨论(0)
  • 2021-02-05 01:15

    This code:

    System.IO.File.ReadAllLines(path)
    

    returns an array of strings. The correct code is:

    System.IO.File.ReadAllText(path)
    
    0 讨论(0)
提交回复
热议问题