Excel导出XML格式文件

8 篇文章 0 订阅

在读取配置文件的时候经常会用到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格式文件

<?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;
                    }
                }
            }
        }
    }

 

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值