Unityで使う時
XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load("Path");
しかし、この場合のパスはシステムのパスになってしまう。
Load()の引数にはパス名を入れる。XMLのテキストデータの文字ではない。
XMLのテキストデータの文字を使う場合は
↓のようにする
インポート
using System; using System.Collections; using System.IO; using System.Xml;
string XML_PATH = "XML/Filename"; // .xmlはいらない! XmlDocument xmlDoc = new XmlDocument(); TextAsset textAsset = Resources.Load(XML_PATH) as TextAsset; try { xmlDoc.LoadXml(textAsset.ToString()); XmlNodeList nodeList = xmlDoc.SelectNodes("root/item/child"); foreach (XmlNode node in nodeList) { Debug.Log("name:" + node.Name); Debug.Log("text:" + node.InnerText); } } catch (Exception ex) { Debug.LogError(ex.Message); }
参考URL:http://wiki.livedoor.jp/bokkuri_orz/d/C%23%20-%20XML
XMLは参考先を真似したこうした。
<?xml version="1.0" encoding="utf-8"?> <root> <item attr="value"> <!--comment--> </item> <item> <child>child 1</child> <child>child 2</child> </item> </root>
Text node cannot appear in this state. Line 1, position 1.
っていううざいエラー
ASCIIにしたらOKぽかった
// settings.Encoding = Encoding.UTF8;
settings.Encoding = Encoding.ASCII;