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
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();
streamReader.Close();
memoryStream.Close();
return result;
}