1.首先在XML中找到这个XML文件。
例如:
character.xml
code:
string path = “character”;//需要读取的XML文件的名字
Hashtable xmldata=null;//定义一个哈希表接收已经反序列化成功的xml文件
private Hashtable ReadBinary(string path)
{
if(xmldata==null || !xmldata.Contains())
return null;
return xmldata[path] as Hashtable;
}
2.character.xml的具体内容,一般由数值策划填写Excel后转化为XML
<characters>
<effect id="1" name=“塞巴斯蒂安” desc=“恶魔执事”/>
<effect id="2" name=“巴卫” desc=“傲娇妖仙”/>
<effect id="3" name=“鲁鲁修” desc=“妹控之魂”/>
<effect id="4" name=“L” desc=“智商碾压”/>
</characters>
3.用下面的方法读取数据
private string ReadData(object data,string Name) {
string value="";
if(data is XmlElement){
if(((XmlElement)data).HasAttribute(Name)==false)
{
return null ;
}
else
{
value = ((XmlElement)data).Attributes[Name].InnerText ;
}
}
return value;
}
4.先提取每张表格的共同点,写一个数据模型的基类
using System;
using UnityEngine;
using System.Xml;
using System.Collections;
[Serializable()]
public class BasicModel{
protected string _name;
protected int _id ;
protected string _desc ;
protected object _key;
public virtual void InitByData(object data)
{
_id = "0" + Tools.ReadData(data,"id") ;
_name = Tools.ReadData(data,"name");
_key = _id;
}
public object key{
get{return _key;}
}
public virtual string desc{
get{return _desc;}
}
public virtual string name{
get{return _name;}
}
public virtual int id{
get{return _id;}
}
}
5.写出character的数据模型,继承基类
using UnityEngine;
using System;
using System.Xml;
using System.Collections;
[Serializable()]
public class CharacterModel : BasicModel
{
private string _desc;
public CharacterModel (){
}
public override void InitByData(object data){
_desc = Tools.ReadField(data,”desc”);
base.InitByData(data);
}
public string desc {get{return _desc;}}
}
要用到表格里的属性的时候,用CharacterModel即可。
原文链接:https://www.f2er.com/xml/296435.html