Loading xml with encoding UTF 16 using XDocument Loading xml with encoding UTF 16 using XDocument xml xml

Loading xml with encoding UTF 16 using XDocument


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);}


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

XDocument xdoc = XDocument.Parse(System.IO.File.ReadAllLines(path));


This code:

System.IO.File.ReadAllLines(path)

returns an array of strings.The correct code is:

System.IO.File.ReadAllText(path)