XmlException: Text node canot appear in this state

I'm running into the following exception when trying to load an xml file:

XmlException: Text node cannot appear in this state. Line 1, position 1. Mono.Xml2.XmlTextReader.ReadText (Boolean notWhitespace) Mono.Xml2.XmlTextReader.ReadContent () Mono.Xml2.XmlTextReader.Read () System.Xml.XmlTextReader.Read () System.Xml.XmlDocument.ReadNodeCore (System.Xml.XmlReader reader) System.Xml.XmlDocument.ReadNode (System.Xml.XmlReader reader) System.Xml.XmlDocument.Load (System.Xml.XmlReader xmlReader) System.Xml.XmlDocument.LoadXml (System.String xml)

My first thought was that since I'm using an xml TextAsset, the importer was converting the encoding for runtime (so it was perhaps borking on the encoding section of the header?). I tried switching the encoding to "ascii" to see if that made a difference, but it didn't. I then tried removing the encoding attribute altogether, but that didn't matter either. I'm starting to think it's not the encoding that's the problem :p

Here is the xml that I'm trying to parse:

<?xml version="1.0" encoding="utf-8"?>
<Graph Name="">
  <State Name="1" LocationX="550" LocationY="219.723333333333" />

And here is my code that parses it. blueprint is a public property that is linked to the xml asset using the unity editor.

System.Xml.XmlDocument doc = new System.Xml.XmlDocument();

I've also tried using an XmlTextReader, but it had the same error (they use the same function calls internally):

System.IO.StringReader stringReader = new System.IO.StringReader(blueprint.text);
System.Xml.XmlReader reader = System.Xml.XmlReader.Create(stringReader);

This solution works for me:

System.IO.StringReader stringReader = new System.IO.StringReader(textAsset.text);
stringReader.Read(); // skip BOM
System.Xml.XmlReader reader = System.Xml.XmlReader.Create(stringReader);

By skipping the first character (which in Unity always seems to be the BOM) the rest of the stream can be read just fine.

It is indeed the UTF-8 encoding that is causing the error. By converting the file to ascii (essentially removing the BOM), unity was able to use it. I had been under the impression that Unity works with utf-8 text files. Am I wrong about that?

* Returns safe text from TextAsset.
* Text files can contain byte order mark (BOM) to specify encoding details.
* Generally, BOM is consumed when loading text from a file (for example with TextReader or XmlReader).
* TextAsset provides “text” field that contains “raw” file text where BOM is preserved.
* This can cause errors.
* For example, when trying to read xml with XmlReader.
* (XmlException: Text node cannot appear in this state. Line 1, position 1.
* Mono.Xml2.XmlTextReader.ReadText (Boolean notWhitespace)… )
* */
public static string GetTextWithoutBOM(TextAsset textAsset)
MemoryStream memoryStream = new MemoryStream(textAsset.bytes);
StreamReader streamReader = new StreamReader(memoryStream, true);

		string result = streamReader.ReadToEnd();
		return result;

Just to check, there isn't a blank line or space before the part on the first line, is there?

I had this problem also. What I did is saved my XML file as UTF-8 without BOM and it worked correctly. (I did this using notepad++)

// fix for unity stupid BOM problem with UTF-8 encoding
string xmlString = System.IO.File.ReadAllText(path);

@Azound Hope this helps you somehow :slight_smile: It worked for me great.

I find solution for this problem is use XML Editor to write xml file. i used EditiX.
and error disappears.

Open xmp in notepad
Click ‘saveas’
Extention: ALL(*)
Name: AndroidManifest.xml

Problem solved.