在读取配置文件的时候经常会用到XML格式的数据,然而XML格式文件配置起来极不方便,使用excel将配置文件数据导出为XML文件是一种很好的解决方案。
首先准备好xml模板文件,每一条数据包含sourcename/man/woman三个字段:
<?xml version="1.0" encoding="utf-8"?>
<root>
<item ID="">
<sourcename/>
<man/>
<woman/>
</item>
<item ID="">
<sourcename/>
<man/>
<woman/>
</item>
</root>
创建一个excel文件,选择文件--选项--自定义功能区,勾选上开发工具
添加后在菜单栏会多出开发工具选项
选择 源--XML映射,添加上面编写完成的xml格式模板
右键选择映射元素即可完成excel文件格式的初始化
完成数据配置后导出xml格式文件
扫描二维码关注公众号,回复:
15429693 查看本文章
<?xml version="1.0" encoding="UTF-8" standalone="true"?>
-<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
-<item ID="1">
<sourcename>刘</sourcename>
<man>帅帅</man>
<woman>花花</woman>
</item>
-<item ID="2">
<sourcename>张</sourcename>
<man>皮皮</man>
<woman>美美</woman>
</item>
-<item ID="3">
<sourcename>王</sourcename>
<man>壮壮</man>
<woman>翠翠</woman>
</item>
</root>
至此,xml格式文件导出完成。
xml格式文件读取方式如下:
private List<string> sourceNameList = new List<string>();
private List<string> manNameList = new List<string>();
private List<string> womanNameList = new List<string>();
public void InitRDNameCfg()
{
TextAsset xml = Resources.Load<TextAsset>(PathDefine.RdNamePath);
if (!xml)
{
Debug.LogError("xml file" + PathDefine.RdNamePath + "not exist");
}
else
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xml.text);
XmlNodeList nodeList = xmlDoc.SelectSingleNode("root").ChildNodes;
for (int i = 0; i < nodeList.Count; ++i)
{
XmlElement element = nodeList[i] as XmlElement;
int ID = Convert.ToInt32(element.GetAttributeNode("ID").InnerText);
foreach (XmlElement ele in element.ChildNodes)
{
switch (ele.Name)
{
case "sourcename":
sourceNameList.Add(ele.InnerText);
break;
case "man":
manNameList.Add(ele.InnerText);
break;
case "woman":
womanNameList.Add(ele.InnerText);
break;
}
}
}
}
}