44

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

<?xml version="1.0" encoding="utf-16"?>

When i removed encoding manually.It works perfectly.

I am getting error " There is no Unicode byte order mark. Cannot switch to Unicode. "

i tried searching and i landed up here-->

Why does C# XmlDocument.LoadXml(string) fail when an XML header is included?

But could not solve my problem.

My code :

XDocument xdoc = XDocument.Load(path);

Any suggestions ??

thank you.

Community
  • 1
  • 1
Sangram Nandkhile
  • 17,634
  • 19
  • 82
  • 116

3 Answers3

68

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);
}
Randy Levy
  • 22,566
  • 4
  • 68
  • 94
  • I have my the output of XML being spit into html. The above solution did not work for me. I have a copyright symbol (c) in my text of the xml document and it always gets garbled when the HTML comes to the browser. So I tried to encode (XML encode the HTML encoded string) but it literally comes out as it is in HTML. i.e. &copy; in xml literally comes out as &copy; instead of the expected © so that the browser can display the (c). – Moiz Tankiwala Feb 27 '15 at 12:24
10

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

XDocument xdoc = XDocument.Parse(System.IO.File.ReadAllLines(path));
Sangram Nandkhile
  • 17,634
  • 19
  • 82
  • 116
  • 1
    Wow! This is new, 4.5 was just released! (2012-08-15 - 5 days ago) :) Way to stay up on your framework Sangram! – Arvo Bowen Aug 20 '12 at 15:00
  • From what I can tell the XDocument.Parse() method was new and just introduced to .Net 4.5 framework. That was released on 2012-08-15 which at the time was 5 days ago... It was simply a complement, usually people just say thanks for a complement. :P – Arvo Bowen Aug 30 '12 at 14:17
  • Okay. Thanks for that :) but i think existed from 3.5 because i am still using 3.5 :P check http://msdn.microsoft.com/en-us/library/system.xml.linq.xdocument.parse%28v=vs.90%29.aspx – Sangram Nandkhile Aug 31 '12 at 07:15
  • Well then I stand corrected! :) When you search for "XDocument.Parse()" on Google it shows you the 4.5 framework pages only... :/ And the reason I was so quick to accept that was because I use 4.0 and for some reason it was not showing up natively... Now that I go back and try it again, it seems to be working fine... So I guess I retract my original comment. :P Not sure what I was smoking that day... Sorry ;) – Arvo Bowen Aug 31 '12 at 14:45
  • LOL !! no need to be sorry. at least we both learned something new. :) – Sangram Nandkhile Sep 03 '12 at 06:45
  • 4
    This doesn't compile for me as the Parse method accepts a string and the ReadAllLines method returns a string array! Shouldn't it be ReadAllText? – Iain Ward Mar 21 '13 at 12:00
  • 2
    XDocument.Parse expects a string, so File.ReadAllLines doesn't compile. You have to use File.ReadAllText. At least, that's how it's working for me in VS2017. – Tyler Jones Jun 30 '17 at 22:28
7

This code:

System.IO.File.ReadAllLines(path)

returns an array of strings. The correct code is:

System.IO.File.ReadAllText(path)
Jonatan
  • 2,734
  • 2
  • 22
  • 33