- Home /
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" />
</Graph>
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();
doc.LoadXml(blueprint.text);
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);
Regarding the same exact error. I have been testing a method to replace a functional runtime assets loading system using Resources.Load for www to download xml and image files. After solving the crossdomain.xml issue and reading everything I have found in the forums (no "not visited" links for days. Well..., chinese ones, sorry for my "newbieness", I'm sure there must be something I'm missing).
Files saved with notepad++ (no BO$$anonymous$$ UTF-8). Code of X$$anonymous$$L file starts with: (with no visible spaces)
Relevant code is here:
(...)
else{
//if source is NOT local for standalone deployment, path values provided in inspector
pathPrefix = "http://";
resourcesPath = pathPrefix + remoteResourcesPath + xmlSource;
monitorGUI3.guiText.text = "resourcesPath: " + resourcesPath;
}
var xmlPath = System.OI.Path.Combine( resourcesPath , xmlName + ".xml");
// Start a download of the given URL
monitorGUI3.guiText.text = "Path: " + xmlPath;
var wwwD : WWW = new WWW (xmlPath);
// Wait for download to complete
yield wwwD;
if (wwwD.error != null){
monitorGUI3.guiText.text = "Can't access path: " + xmlPath;
monitorGUI4.guiText.text = wwwD.error;
Debug.Log("WWW error:" + wwwD.error);
}
else{
monitorGUI3.guiText.text = "Data accessed at path: " + xmlPath;
//X$$anonymous$$L is showing in guitext & console at runtime, despite the error, as the next lines require
monitorGUI4.guiText.text = wwwD.text;
Debug.Log("X$$anonymous$$L data:" + wwwD);
var xmlDocWeb = new XmlDocument();
//var xmlImportedData : TextAsset;//unused after reading webplayer loads xml as string
var xmlExtracted : String;
xmlExtracted = wwwD.text;
xmlDocWeb.LoadXml(xmlExtracted);
monitorGUI3.guiText.text = "X$$anonymous$$L from: " + xmlPath;
Debug.Log("Url at " + xmlPath);
(...)
Getting this error
XmlException: Text node cannot appear in this state. Line 1, position 1. $$anonymous$$ono.Xml2.XmlTextReader.ReadText (Boolean notWhitespace) $$anonymous$$ono.Xml2.XmlTextReader.ReadContent () $$anonymous$$ono.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)
www.data tried as well, but I get a message indicating that www.text should be used
xmlDocument.Load didn't work neither with www.text nor with www, but after reading I shouldn't be trying to convert to TextAsset for web player, I started testing xmlDocument.LoadXml
Thanks to all that post and answer questions, it's of great help.
@levelDee: In a webbuild not all constructors of the WebRequest class are supported for security reasons. The XmlDocument.Load method seems to use one internally that isn't supported.
Personally i never had much issues with loading a X$$anonymous$$L file with the WWW class. Are you sure that your file doesn't have a BO$$anonymous$$ at the start? Since your X$$anonymous$$LParser clearly complains about the first character in the file (Line 1, position 1).
Try loading the file with the WWW class and view the first bytes as hex:
var www = new WWW("YourURL");
yield www;
var data = www.bytes;
var res = "";
for(var i = 0; i < 10; i++)
res += data[i].ToString("X2"))+" ";
Debug.Log(res);
Try this and tell us what you see in the console (copy the result in a comment here).
Thanks, I'll try right away. Notepad++ says it's converted to UTF-8 w/o BO$$anonymous$$. Will post resumed tests after trying.
EDITED for inclusion of accdidentally removed previous comment.
Debug after adding lines:
3C 3F 78 6D 6C 20 76 65 72 73 UnityEngine.Debug:Log(Object) $:$$anonymous$$oveNext()
2nd EDIT: It must be as you said, no XmlDocument.Load allowed, so I replaced it for XmlDocument.XmlLoad
var xmlDocWeb : XmlDocument = new XmlDocument();
xmlDocWeb.LoadXml(xmlPath);
Error:
*(Filename: C:/BuildAgent/work/d63dfc6385190b60/artifacts/WebPlayerGenerated/UnityEngineDebug.cpp Line: 49) 3C 3F 78 6D 6C 20 76 65 72 73
(Filename: C:/BuildAgent/work/d63dfc6385190b60/artifacts/WebPlayerGenerated/UnityEngineDebug.cpp Line: 49) Platform assembly: C:\Users\Dev\AppData\LocalLow\Unity\WebPlayer\mono\Release3.x.x\Data\lib\System.dll (this message is harmless) XmlException: Text node cannot appear in this state. Line 1, position 1. at $$anonymous$$ono.Xml2.XmlTextReader.ReadText (Boolean notWhitespace) [0x00000] in :0 at $$anonymous$$ono.Xml2.XmlTextReader.ReadContent () [0x00000] in :0 at $$anonymous$$ono.Xml2.XmlTextReader.Read () [0x00000] in :0 at System.Xml.XmlTextReader.Read () [0x00000] in :0 at System.Xml.XmlDocument.ReadNodeCore (System.Xml.XmlReader reader) [0x00000] in :0 at System.Xml.XmlDocument.ReadNode (System.Xml.XmlReader reader) [0x00000] in :0 at System.Xml.XmlDocument.Load (System.Xml.XmlReader xmlReader) [0x00000] in :0 at System.Xml.XmlDocument.LoadXml (System.String xml) [0x00000] in :0 at DataGUI+$leerXml$$anonymous$$odulo$132+$.$$anonymous$$oveNext () [0x00000] in :0
(Filename: Line: -1)*
Stuck between the same options... Should I replace for another method? or maybe parse it with C#?
Seems to be working now with LoadXml, but calling the www.data, despite the console message. Tested with web player compiled, uploaded, and calling xml files to load text and image paths (also located online).
Cleaning up files and paths now. I will post some results when done. Thanks for the tip and test code, that left that particular issue out, as the first chars seem valid.
Answer by Azound · Feb 06, 2010 at 06:16 AM
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?
I resolved this issue by opening my X$$anonymous$$L file in Notepad++ and changing the encoding to "Encode in UTF-8 without BO$$anonymous$$"
Answer by Orion 1 · Jun 01, 2010 at 07:39 AM
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.
Thank you for the "Read" trick/ for my part I was using XmlDocument:
XmlDocument xmldoc = new XmlDocument(); System.IO.StringReader stringReader = newSystem.IO.StringReader(iXmlText); stringReader.Read(); // skip BO$$anonymous$$
xmldoc.LoadXml(stringReader.ReadToEnd());
NOAA_Julien, as far as I understand, You will not parse the text twice as Read() for StringReader reads only first character. Not sure about copying the text in constructor though.
Thanks a lot , without stringReader.Read(); this does totally not work for me.
Answer by Maverick · Apr 20, 2010 at 11:09 AM
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++)
I'm having this same problem, except that encoding it as UTF-8 without BO$$anonymous$$ using Notepad++ is not fixing it.
This is my X$$anonymous$$L file:
<Cards>
<Card id="0" name="Evangelize" castingCost="1" moneyCost="10" range="5" targetsType="space/Holy Space" AOE="single" effects="create/Follower" tags="preemptive" commonality="30" art="none" description="Choose a holy space you own; place a new follower there. This new follower may not pray this turn."></Card>
</Cards>
And this is the code I'm using to import it:
string loadFrom = Application.dataPath + @"/X$$anonymous$$L/Cards.xml";
Debug.Log(" loadFrom = " + loadFrom );
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml ( loadFrom );
What am I missing?
@yunatee: The LoadXml method expects an X$$anonymous$$L string, not a file name. You have to load the content and pass the content to LoadXml.
You can do this with System.IO
if you're targetting standalone or mobile. Just use the ReadAllText method:
string content = System.IO.File.ReadAllText( loadFrom );
xmlDoc.LoadXml( content );
If you targetting webGL / webplayer you have to use the WWW class since you can't access the local file system from those at all.
Answer by andrey_bryzgalov · Feb 26, 2012 at 12:06 AM
/**
* 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;
}
Answer by andeeee · Feb 02, 2010 at 05:04 PM
Just to check, there isn't a blank line or space before the part on the first line, is there?
Your answer
Follow this Question
Related Questions
How to refresh assets during run time of a build ? 0 Answers
Load xml from resource folder after build(web player) 0 Answers
How to use XML Serialization to load custom asset types 3 Answers
Refresh assets during build. 1 Answer
Load issues 2 Answers