加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

对XML中的数据初步处理

发布时间:2020-12-16 06:13:47 所属栏目:百科 来源:网络整理
导读:1.首先在XML中找到这个XML文件。 例如: character . xml code: string path = “ character ” ; // 需要读取的 XML 文件的名字 Hashtable xmldata=null;// 定义一个哈希表接收已经反序列化成功的 xml 文件 private Hashtable ReadBinary(string path) { i

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即可。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读