XmlException:文本节点不能出现在此状态(布尔值非空白)。

3

当我尝试从文件创建XDocument时,会出现错误,但问题是它只有20%的时间出现,但我的程序需要每20秒调用创建XDocument的函数,因此这很关键。

下面是从文件加载到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);

错误发生在XDocument.Load()这一行。 实际抛出的异常是:
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)

正如我所说的,它大约80%的时间都能正常工作,剩下的20%会抛出异常。不过,这只意味着该函数在当前迭代中不会运行。等待20秒后它会再次尝试并通常会成功。
我的XML文档应该是完全正常的,以下是一个例子。
<?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>

我读过类似的问题,它们似乎与编码有关,并且尝试过不带BOM的编码,但问题仍然存在。我认为如果大多数情况下都能运行,那么编码不应该是问题。有什么想法吗?

1个回答

3

1
我已经阅读了那篇文章。我在问题的结尾处声明,我尝试过不使用BOM进行编码,但仍然得到相同的结果。我也尝试过UTF-16。 - Dillon Drobena

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接