在Unity3D中使用C#如何进行XML文件的读写

在程序有时候需要从文本中读取数据,或者把数据保存到文件中,使用XML文件来存储数据是一个不错的选择。下面介绍一下在Unity3D中使用C#如何进行XML文件的读写。

1、需要引入的包

[csharp]  view plain copy
  1. using UnityEngine;  
  2. using System.Collections;  
  3. using System.Xml;  

2、编辑你的XML文件

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RoleRoot>  
  3.     
  4.   <Role faction="0">  
  5.     <BaseAttribute   grade="1" force="2" spirit="1" agility="1" endurance="1" wisdom="1"/>  
  6.     <FightAttribute  hp="1" mp="1" generalHurt="1" generalDefense="1" skillHurt="1"  
  7.              skillDefense="1" generalHurtFactor="1" generalDefenseFactor="1"  
  8.                      skillHurtFactor="1" skillDefenseFactor="1"/>  
  9.     <RateAttribute   violenceRate="0" killRate="0" breakDefenseRate="0" duckRate="0"  
  10.                      withstandRate="0" hitRate="0"/>  
  11.     <ValueAttribute  attackOrder="0" money="0" goldBullions="0" bags="0" achievement="0"  
  12.                      experience="0" vitality="0"/>  
  13.   </Role>  
  14. </RoleRoot>  
3、读取XML数据

[csharp]  view plain copy
  1. XmlDocument xmlDoc = new XmlDocument();  
  2.         TextAsset textAsset = (TextAsset)Resources.Load("createRole");        
  3.         xmlDoc.LoadXml(textAsset.text);  
  4.         //xmlDoc.LoadXml(Application.dataPath + @"\createRole.xml");  
  5.         XmlNodeList nodeList = xmlDoc.SelectNodes("RoleRoot/Role");  
  6.         XmlNode node;  
  7.         switch (faction)  
  8.         {  
  9.             case FACTION.Monk:  
  10.                 node = nodeList[0];  
  11.                 break;  
  12.             case FACTION.Taoist:  
  13.                 node = nodeList[1];  
  14.                 break;  
  15.             case FACTION.Flower:  
  16.                 node = nodeList[2];  
  17.                 break;  
  18.             default:  
  19.                 node = nodeList[3];  
  20.                 break;  
  21.         }  
  22.            
  23.         //XmlNode baseAttribute=node.SelectNodes("BaseAttribute");  
  24.         XmlNode baseAttribute = node.ChildNodes[0];  
  25.         grade = XmlConvert.ToInt32(baseAttribute.Attributes["grade"].Value);  
  26.         force = XmlConvert.ToInt32(baseAttribute.Attributes["force"].Value);  
  27.         spirit = XmlConvert.ToInt32(baseAttribute.Attributes["spirit"].Value);  
  28.         agility = XmlConvert.ToInt32(baseAttribute.Attributes["agility"].Value);  
  29.         endurance = XmlConvert.ToInt32(baseAttribute.Attributes["endurance"].Value);  
  30.         wisdom = XmlConvert.ToInt32(baseAttribute.Attributes["wisdom"].Value);  

在加载xml文件的时候xmlDoc.LoadXML()可能会出现这样的错误

XmlException: Text node cannot appear in this state. Line 1, position 1. 

Mono.Xml2.XmlTextReader.ReadText (Boolean notWhitespace) 

Mono.Xml2.XmlTextReader.ReadContent () Mono.Xml2.XmlTextReader.Read () 

出现这个错误的原因是因为Unity3D加载XML文件的时候,XML文件必须保存为UTF-8编码的格式,同时还必须去掉开头的两个字节(BOM)用来标识UTF-8用的。这时你可以选择一些编辑工具另存为UTF-8,(有些工具默认的会为UTF-8编码添加一个BOM标识),比如你可以使用eclipse导出xml,这样就不含有BOM标识。还有其他一些解决办法:给个链接吧:http://answers.unity3d.com/questions/10904/xmlexception-text-node-canot-appear-in-this-state.html
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值