查看: 1031|回复: 3
打印 上一主题 下一主题

[其他] unity c#读取xml

[复制链接]

2317

主题

54

听众

2万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
20645
精华
62

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2014-12-31 23:00:52 |只看该作者 |倒序浏览
  1. using UnityEngine;

  2. using System.Collections;

  3. using System.Xml;

  4. using System.IO;

  5. public class ReadXml : MonoBehaviour {

  6. private string[] myNames;

  7. // Use this for initialization

  8. void Start () {

  9. LoadMyXML();

  10. }

  11. // Update is called once per frame

  12. void Update () {

  13. }

  14. private void LoadMyXML()

  15. {

  16. //xml存放路径

  17. string filePath = Application.dataPath + @“/testxml.xml”;

  18. Debug.Log (“filePath=” + filePath);

  19. if (File.Exists(filePath))

  20. {

  21. //声明个xml文档

  22. XmlDocument xmlDocument = new XmlDocument();

  23. XmlReaderSettings settings = new XmlReaderSettings();

  24. settings.IgnoreComments = true;//忽略文档里面的注释

  25. xmlDocument.Load(XmlReader.Create(filePath,settings));

  26. //获取名称为myRoot的根节点

  27. XmlNodeList xmlNodeList = xmlDocument.SelectSingleNode(“myRoot”).ChildNodes;

  28. //Debug.Log(xmlNodeList.Count);

  29. //遍历子节点

  30. Debug.Log(“readxml”);

  31. foreach (XmlElement xmlElement1 in xmlNodeList)

  32. {

  33. if (xmlElement1.Name == “myChild”)

  34. {

  35. int index = 0;

  36. myNames = new string[xmlElement1.ChildNodes.Count];

  37. //Debug.Log(xmlElement1.ChildNodes.Count);

  38. foreach (XmlElement xmlElement2 in xmlElement1.ChildNodes)

  39. {

  40. myNames[index] = xmlElement2.InnerText;

  41. Debug.Log(myNames[index]);

  42. index++;

  43. }

  44. }

  45. }

  46. }

  47. }

  48. }

  49. xml文件

  50. <?xml version=“1.0” encoding=“UTF-8”?>

  51. <myRoot>

  52. <myChild>

  53. <myName>张三</myName>

  54. <myName>李四</myName>

  55. <myName>王老五</myName>

  56. <myName>刘邦</myName>

  57. <myName>项羽</myName>

  58. <myName>秦始皇</myName>

  59. <!--记录书本的信息-->

  60. </myChild>

  61. </myRoot>
复制代码
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

100

主题

3

听众

7683

积分

高级设计师

Rank: 6Rank: 6

纳金币
2378
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

沙发
发表于 2014-12-31 23:24:47 |只看该作者
涨姿势了。。。。。。。。
回复

使用道具 举报

47

主题

2

听众

2810

积分

中级设计师

Rank: 5Rank: 5

纳金币
571
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

板凳
发表于 2015-1-2 19:22:07 |只看该作者
不错的C#代码
回复

使用道具 举报

16

主题

1

听众

1万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
3
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

地板
发表于 2015-1-3 11:08:05 |只看该作者
不错, 学习了!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

关闭

站长推荐上一条 /1 下一条

手机版|纳金网 ( 闽ICP备08008928号

GMT+8, 2024-5-15 07:42 , Processed in 0.085027 second(s), 32 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部