在Unity3D中使用C#如何进行XML文件的读写
2016-08-04 17:16
447 查看
<span style="font-family:Arial;"><span style="background-color: rgb(255, 255, 255);">转载地址:http://blog.csdn.net/qq_17668637/article/details/49155447</span></span>
1、需要引入的包
using UnityEngine;
using System.Collections;
using System.Xml; 2、编辑你的XML文件
<?xml version="1.0" encoding="utf-8"?>
<RoleRoot>
<Role faction="0">
<BaseAttribute grade="1" force="2" spirit="1" agility="1" endurance="1" wisdom="1"/>
<FightAttribute hp="1" mp="1" generalHurt="1" generalDefense="1" skillHurt="1"
skillDefense="1" generalHurtFactor="1" generalDefenseFactor="1"
skillHurtFactor="1" skillDefenseFactor="1"/>
<RateAttribute violenceRate="0" killRate="0" breakDefenseRate="0" duckRate="0"
withstandRate="0" hitRate="0"/>
<ValueAttribute attackOrder="0" money="0" goldBullions="0" bags="0" achievement="0"
experience="0" vitality="0"/>
</Role>
</RoleRoot>
<span style="font-family: Arial; background-color: rgb(255, 255, 255);"> </span>
<span style="font-family: Arial; background-color: rgb(255, 255, 255);">在程序有时候需要从文本中读取数据,或者把数据保存到文件中,使用XML文件来存储数据是一个不错的选择。下面介绍一下在Unity3D中使用C#如何进行XML文件的读写。</span>
1、需要引入的包
using UnityEngine;
using System.Collections;
using System.Xml; 2、编辑你的XML文件
<?xml version="1.0" encoding="utf-8"?>
<RoleRoot>
<Role faction="0">
<BaseAttribute grade="1" force="2" spirit="1" agility="1" endurance="1" wisdom="1"/>
<FightAttribute hp="1" mp="1" generalHurt="1" generalDefense="1" skillHurt="1"
skillDefense="1" generalHurtFactor="1" generalDefenseFactor="1"
skillHurtFactor="1" skillDefenseFactor="1"/>
<RateAttribute violenceRate="0" killRate="0" breakDefenseRate="0" duckRate="0"
withstandRate="0" hitRate="0"/>
<ValueAttribute attackOrder="0" money="0" goldBullions="0" bags="0" achievement="0"
experience="0" vitality="0"/>
</Role>
</RoleRoot>
<span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 18px; line-height: 26px;">3、读取XML数据</span>
<span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 18px; line-height: 26px;"></span><pre name="code" class="csharp">XmlDocument xmlDoc = new XmlDocument(); TextAsset textAsset = (TextAsset)Resources.Load("createRole"); xmlDoc.LoadXml(textAsset.text); //xmlDoc.LoadXml(Application.dataPath + @"\createRole.xml"); XmlNodeList nodeList = xmlDoc.SelectNodes("RoleRoot/Role"); XmlNode node; switch (faction) { case FACTION.Monk: node = nodeList[0]; break; case FACTION.Taoist: node = nodeList[1]; break; case FACTION.Flower: node = nodeList[2]; break; default: node = nodeList[3]; break; } //XmlNode baseAttribute=node.SelectNodes("BaseAttribute"); XmlNode baseAttribute = node.ChildNodes[0]; grade = XmlConvert.ToInt32(baseAttribute.Attributes["grade"].Value); force = XmlConvert.ToInt32(baseAttribute.Attributes["force"].Value); spirit = XmlConvert.ToInt32(baseAttribute.Attributes["spirit"].Value); agility = XmlConvert.ToInt32(baseAttribute.Attributes["agility"].Value); endurance = XmlConvert.ToInt32(baseAttribute.Attributes["endurance"].Value); wisdom = XmlConvert.ToInt32(baseAttribute.Attributes["wisdom"].Value);
<p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"><span style="font-size: 18px;">在加载xml文件的时候xmlDoc.LoadXML()可能会出现这样的错误</span></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"><span style="font-size: 18px;"><span style="font-family: 'helvetica neue', sans-serif; font-size: 13px; line-height: 18px; background-color: rgb(245, 245, 245);">XmlException: <span class="hilite1">Text</span> <span class="hilite2">node</span> <span class="hilite3">cannot</span> <span class="hilite4">appear</span> <span class="hilite5">in</span> <span class="hilite6">this</span> <span class="hilite7">state</span>. Line 1, position 1. </span></span></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"><span style="font-size: 18px;"><span style="font-family: 'helvetica neue', sans-serif; font-size: 13px; line-height: 18px; background-color: rgb(245, 245, 245);">Mono.Xml2.XmlTextReader.ReadText (Boolean notWhitespace) </span></span></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;"><span style="font-size: 18px;"><span style="font-family: 'helvetica neue', sans-serif; font-size: 13px; line-height: 18px; background-color: rgb(245, 245, 245);">Mono.Xml2.XmlTextReader.ReadContent () Mono.Xml2.XmlTextReader.Read () <img src="http://my.csdn.net/uploads/201207/13/1342167310_8634.jpg" alt="" style="border: none; max-width: 100%;" /></span> </span></p><span style="color: rgb(51, 51, 51); font-family: Arial; line-height: 26px; font-size: 18px;">出现这个错误的原因是因为Unity3D加载XML文件的时候,XML文件必须保存为UTF-8编码的格式,同时还必须去掉开头的两个字节(BOM)用来标识UTF-8用的。这时你可以选择一些编辑工具另存为UTF-8,(有些工具默认的会为UTF-8编码添加一个BOM标识),比如你可以使用eclipse导出xml,这样就不含有BOM标识。还有其他一些解决办法:给个链接吧:<a target=_blank target="_blank" href="http://answers.unity3d.com/questions/10904/xmlexception-text-node-canot-appear-in-this-state.html" style="color: rgb(255, 153, 0); text-decoration: none;">http://answers.unity3d.com/questions/10904/xmlexception-text-node-canot-appear-in-this-state.html</a></span>
相关文章推荐
- 在Unity3D中使用C#如何进行XML文件的读写
- C#使用DOM进行XML文件的读写操作介绍
- 如何使用C#进行Visio二次开发-总结版
- 如何使用C#进行Visio二次开发
- C#如何使用REST接口读写数据
- java 如何使用缓冲区对文件进行读写操作
- 如何使用C#进行PPT展示。
- 如何使用C#进行Visio二次开发
- android基础---如何使用sharedPreference进行多个Activity之间的读写操作
- C#如何把类CLASS实例进行序列化及反序列化为XML文件
- unity3d 使用c#如何访问某个对象里的c#脚本里的一个变量
- C#如何读写xml文件
- 使用c#如何读取xml文件
- 使用DOM进行XML文件的读写操作
- 【MySQL】如何使用C#+MySQL实现一条MySQL语句进行多表查询
- 如何使用C#对虚拟磁盘文件(VHD)进行操作
- 如何使用C#进行Visio二次开发
- VB6中如何使用C#开发的WebService进行开发
- [ZT]如何使用C#进行Visio二次开发
- 元旦快乐,阖家团圆,幸福安康.C#重载示例(有问重载该如何选择?在C#中可很方便地在智能感知弹出中选择不同参数列表进行使用;不像C/C++那样,要记住编译器自动选择最佳匹配参数列表的概念)