Unity3D 读取XML文件里面的属性

来源:互联网 发布:合肥师范学院行知学堂 编辑:程序博客网 时间:2024/05/01 08:20

本文固定链接:http://blog.csdn.net/u013108312/article/details/62045236

Mono.xml 下载链接:http://download.csdn.net/detail/cwqcwk1/7105071


<?xml version="1.0"?><info>    <book id="b1" lang="en">        <name>c++</name>        <price>570</price>    </book>    <book id="b2" lang="en">        <name>c#</name>        <price>110</price>    </book></info>

Unity3D 读取XML文件里面的属性

using Mono.Xml;using System.Collections;using System.Collections.Generic;using System.IO;using System.Security;using System.Xml;using UnityEngine;public class ReadXML : MonoBehaviour {    // Use this for initialization    void Start () {        //TextAsset ta = Resources.Load("test", typeof(TextAsset)) as TextAsset;        //ReadXMlTest(new MemoryStream(ta.bytes));        StartCoroutine(StartTxt());    }    // Update is called once per frame    void Update () {    }    IEnumerator StartTxt()    {        WWW www = new WWW("file://" + Application.streamingAssetsPath + "/test.xml");        yield return www;        //ReadXMlTest(new MemoryStream(www.bytes));        ReadXMLMono(www.text);        www.Dispose();    }    void ReadXMlTest(Stream stream)    {        XmlDocument xmldoc = new XmlDocument();        xmldoc.Load(stream);        XmlNode info = xmldoc.SelectSingleNode("info");        foreach (XmlNode node in info.ChildNodes)        {            string id = node.Attributes["id"].Value;            string lang = node.Attributes["lang"].Value;            string name = node.SelectSingleNode("name").InnerText;            string price = node.SelectSingleNode("price").InnerText;            Debug.Log("node.Name:" + node.Name + " id:"+ id + " lang:" + lang + " name:" + name + " price:" + price);        }    }    void ReadXMLMono(string text)    {        SecurityParser sp = new SecurityParser();        sp.LoadXml(text);        SecurityElement se = sp.ToXml();        foreach (SecurityElement sel in se.Children)        {            string id = (string)sel.Attributes["id"];            string lang = (string)sel.Attributes["lang"];            string name = "", price = "";            foreach (SecurityElement se2 in sel.Children)            {                if (se2.Tag == "name")                {                    name = se2.Text;                }                else if (se2.Tag == "price")                {                    price = se2.Text;                }            }            Debug.Log(" id:" + id + " lang:" + lang + " name:" + name + " price:" + price);        }    }}
1 0
原创粉丝点击