Unity3d XmlException: Text node cannot appear in this state的方案
這個問題是utf-8編碼的問題,如果是utf-8+bom那么就會出現這種問題,如果是單純的utf-8就沒有這種問題(當然如果你把他完全變成ansi,那也行)?
我在讀寫xml的時候遇到這個問題。查了好久在老外的一個網上看到了解決的方法。還有我自己總結的一些方法。?
?? ?public bool LoadXml(string xmlFile)?
?? ?{?
?? ? ? ?if (xmlDocument == null)?
?? ? ? ?{?
?? ? ? ? ? ?throw new ArgumentNullException("xmlDocument");?
?? ? ? ?}?
?? ? ? ?if (xmlFile == null || xmlFile.Length == 0)?
?? ? ? ?{?
?? ? ? ? ? ?throw new ArgumentNullException("xmlFile");?
?? ? ? ?}?
// Debug.Log(xmlFile);?
// TextReader tr = new StringReader(xmlFile);?
// ? ? ? ?xmlDocument.Load(tr);?
??
?System.IO.StringReader stringReader = new System.IO.StringReader(xmlFile);?
?stringReader.Read(); // 跳過 BOM?
?System.Xml.XmlReader reader = System.Xml.XmlReader.Create(stringReader);?
?xmlDocument.LoadXml(stringReader.ReadToEnd());?
?? ? ? ?return true;?
??
?? ?}?
這個是bom的解釋網上很多,我不解釋了http://www.cnblogs.com/zhongru_tu/archive/2008/04/11/1147792.html?
這是一個自己寫的導入xml的方法,注釋的部分是我一開始使用的方法,沒有注釋的部分是我修改過的。這樣你在讀取xml的時候就可以跳過bom了.?
如果你不需要發布為網頁形式,那么還有一種方法也可以起到這樣的效果,那就是重新寫一下xml,徹底改變它的編碼方式讓其不帶bom?
代碼如下:?
?Encoding utf8NoBom = new UTF8Encoding(false);?
?string fileString = File.ReadAllText(writeUrl,utf8NoBom);?
?File.WriteAllText(writeUrl,fileString,utf8NoBom);?
注意你需要使用幾個命名空間?
using System.Xml;?
using System.Text;?
using System;?
using System.IO;?
當然有時候你會發現unity雖然你寫了命名空間但是有些類你還是找不到,那么你要到網上下專門的動態庫(dll)放到Plugins文件夾下,當然有些讀寫本地文件的類在發布為網頁形式的時候你是不能夠使用的。?
最后還有一點,你使用了上面的方法以后一定記住把你的xml文件的編碼改為utf-8+bom這種格式,因為我們這個是對bom格式的讀取。?
還有一些其他的小方法你用其他編輯軟件改為utf-8不帶bom的有時候也能解決,但當你改完后,還不能解決的時候,上面的方法對你也許有幫助
轉載于:https://www.cnblogs.com/lancidie/p/9472704.html
總結
以上是生活随笔為你收集整理的Unity3d XmlException: Text node cannot appear in this state的方案的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python 字符串格式化,使用f前缀
- 下一篇: 《JavaScript 高级程序设计》学