gpt4 book ai didi

c# - XmlException 文本节点不能出现在这种状态下(Boolean notWhiteSpace)

转载 作者:数据小太阳 更新时间:2023-10-29 02:29:50 26 4
gpt4 key购买 nike

所以当我尝试从文件创建 XDocument 时出现此错误,但问题是它只发生大约 20% 的时间,但我的程序需要调用这个创建 XDocument 的函数20 秒,所以这是修复的关键。

这是我的函数从文件加载到 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 的情况下对其进行编码,我已经尝试了所有这些,但问题仍然存在。我认为如果它大部分时间都在运行,那么编码应该不是问题。有什么想法吗?

最佳答案

我认为可能是 XML 编码类型(本例为 UTF-8)导致了错误。解决方案是将文件转换为 ascii(去除 BOM),或使用无 BOM 的 UTF-8 编码。

看看这个问题http://answers.unity3d.com/questions/10904/xmlexception-text-node-canot-appear-in-this-state.html为同样的问题寻求帮助。

关于c# - XmlException 文本节点不能出现在这种状态下(Boolean notWhiteSpace),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28059450/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com