So I'm having this error occur when I try to create an XDocument from a file, but the issue is that it occurs only about 20% of the time, but my program needs to call this function that creates the XDocument every 20 seconds, so this is critical to fix.
This is a small snippit of my function loading from the file into the XDocument
//Read all patterns
DirectoryInfo directory = new DirectoryInfo ("Assets/_Scripts/Items/Orb Patterns");
orbPatterns = directory.GetFiles ().Cast<FileInfo> ().ToList ();
//Pick a random pattern
XDocument xmlDoc = XDocument.Load (orbPatterns [Random.Range (0, orbPatterns.Count - 1)].FullName);
The error occurs on the XDocument.Load() line.
The actual exception being thrown is
XmlException: Text node cannot appear in this state.
file:///Assets/_Scripts/Items/Orb Patterns/pattern1.xml.meta Line 1, position 1.
Mono.Xml2.XmlTextReader.ReadText (Boolean notWhitespace)
Mono.Xml2.XmlTextReader.ReadContent ()
Mono.Xml2.XmlTextReader.Read ()
System.Xml.XmlTextReader.Read ()
Mono.Xml.XmlFilterReader.Read ()
System.Xml.Linq.XDocument.ReadContent (System.Xml.XmlReader reader, LoadOptions options)
System.Xml.Linq.XDocument.LoadCore (System.Xml.XmlReader reader, LoadOptions options)
System.Xml.Linq.XDocument.Load (System.String uri, LoadOptions options)
System.Xml.Linq.XDocument.Load (System.String uri)
As I said, it works about 80% of the time, the other 20% it'll throw the exception. However, this just means the function won't run for the current iteration. After 20 seconds it will try again and usually work.
My XML document should be perfectly fine, here is an example
<?xml version="1.0" encoding="UTF-8"?>
<table>
<cell column="1" row="1">Red</cell>
<cell column="1" row="2">Red</cell>
<cell column="1" row="3">Red</cell>
<cell column="1" row="4">Red</cell>
<cell column="2" row="1">Red</cell>
<cell column="2" row="2">Blue</cell>
<cell column="2" row="3">Blue</cell>
<cell column="2" row="4">Red</cell>
<cell column="3" row="1">Red</cell>
<cell column="3" row="2">Multi</cell>
<cell column="3" row="3">Multi</cell>
<cell column="3" row="4">Red</cell>
<cell column="4" row="1">Red</cell>
<cell column="4" row="2">Blue</cell>
<cell column="4" row="3">Blue</cell>
<cell column="4" row="4">Red</cell>
</table>
I've read similar issues and they seem to be related to the encoding and to encode it without BOM, and I've tried all of that but the issue still occurs. I figure that if it will run most of the time than encoding shouldn't be the issue. Any ideas?
I think that maybe the XML encoding type (UTF-8 is this case) is causing the error. The solution is to convert the file to ascii (removing the BOM), or encoding in UTF-8 without BOM.
Take a look at this question http://answers.unity3d.com/questions/10904/xmlexception-text-node-canot-appear-in-this-state.html which asks help for the same problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With