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