Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>Background</h2> <p>Although your question does have the encoding set as UTF-16, you don't have the string properly escaped so I wasn't sure if you did, in fact, accurately transpose the string into your question.</p> <p>I ran into the same exception: </p> <blockquote> <p>System.Xml.XmlException: Data at the root level is invalid. Line 1, position 1.</p> </blockquote> <p>However, my code looked like this:</p> <pre><code>string xml = "&lt;?xml version=\"1.0\" encoding=\"utf-8\" ?&gt;\n&lt;event&gt;This is a Test&lt;/event&gt;"; XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(xml); </code></pre> <h2>The Problem</h2> <p>The problem is that strings are stored internally as UTF-16 in .NET however the encoding specified in the XML document header may be different. E.g.:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; </code></pre> <p>From the MSDN documentation for String <a href="http://msdn.microsoft.com/en-us/library/system.string.aspx" rel="noreferrer">here</a>:</p> <blockquote> <p>Each Unicode character in a string is defined by a Unicode scalar value, also called a Unicode code point or the ordinal (numeric) value of the Unicode character. Each code point is encoded using UTF-16 encoding, and the numeric value of each element of the encoding is represented by a Char object.</p> </blockquote> <p>This means that when you pass XmlDocument.LoadXml() your string with an XML header, it must say the encoding is UTF-16. Otherwise, the actual underlying encoding won't match the encoding reported in the header and will result in an XmlException being thrown.</p> <h2>The Solution</h2> <p>The solution for this problem is to make sure the encoding used in whatever you pass the Load or LoadXml method matches what you say it is in the XML header. In my example above, either change your XML header to state UTF-16 or to encode the input in UTF-8 and use one of the <a href="http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.load.aspx" rel="noreferrer">XmlDocument.Load methods</a>.</p> <p>Below is sample code demonstrating how to use a MemoryStream to build an XmlDocument using a string which defines a UTF-8 encode XML document (but of course, is stored a UTF-16 .NET string).</p> <pre><code>string xml = "&lt;?xml version=\"1.0\" encoding=\"utf-8\" ?&gt;\n&lt;event&gt;This is a Test&lt;/event&gt;"; // Encode the XML string in a UTF-8 byte array byte[] encodedString = Encoding.UTF8.GetBytes(xml); // Put the byte array into a stream and rewind it to the beginning MemoryStream ms = new MemoryStream(encodedString); ms.Flush(); ms.Position = 0; // Build the XmlDocument from the MemorySteam of UTF-8 encoded bytes XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(ms); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload