I am trying to read the xml document using XDocument method . but i am getting an error when xml has
I tried , and found another way of doing it !!
XDocument xdoc = XDocument.Parse(System.IO.File.ReadAllLines(path));
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);
}
This code:
System.IO.File.ReadAllLines(path)
returns an array of strings. The correct code is:
System.IO.File.ReadAllText(path)