首先,定义一个xml文件,如下:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <ROOT> <table wave="1" level="1" name="John"/> <table wave="2" level="1" name="Lucy"/> </ROOT>把Mono.Xml加进unity3d项目。下载地址: http://download.csdn.net/detail/cwqcwk1/7105071
unity3d利用Mono.xml读取xml的代码如下:
using UnityEngine; using System.Collections; using Mono.Xml; using System.IO; using System.Security; public class XmlLorder { public void Read() { SecurityParser SP = new SecurityParser();
<span style="white-space:pre"> </span>//假设xml文件路径为 Resources/test.xml,其中,不可像原文章一样XMLPATH = "test.xml",此处应为test,不带后缀 string xmlPath = "test";
//调用XML里自带的LoadXML函数
<span style="white-space:pre"> </span>SP.LoadXml(Resources.Load( xmlPath ).ToString());
//ToXml()返回的是XML的根节点 SecurityElement SE = SP.ToXml(); <p> //遍历根节点下的子节点,并取出相应的属性</p><p><span style="white-space:pre"> </span>foreach (SecurityElement child in SE.Children)</p><p><span style="white-space: pre;"> </span>{</p><p><span style="white-space: pre;"> </span>//比对下是否使自己所需要得节点</p>
<span style="white-space:pre"> </span>if(child.Tag == "table") <span style="white-space:pre"> </span>{ //获得节点得属xìng string wave = child.Attribute("wave"); string level = child.Attribute("level"); string name = child.Attribute("name"); Debug.Log("wave:" + wave + " level:" + level + " name:" + name); } } } }