Unity 读取XML文件

本文介绍了一种使用C#语言从XML文件中读取游戏内装备数据的方法,并展示了如何将这些数据存储到内存中的List对象。每件装备包含ID、名称、数量、属性和详细信息等字段。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

	public void ReadData(){//读取XML文件
		string xmlFielPath = Application.dataPath + "/mybackpages/BackPages.xml";//文件路径
		XmlDocument doc = new XmlDocument ();
		doc.Load (xmlFielPath);
		XmlNode root = doc.SelectSingleNode ("Pages");
		foreach (XmlElement item in root) {
			MyPage _m = new MyPage (); //对象,用于管理XML文件内容
			_m.id = item.ChildNodes [0].InnerText;
			_m.equName = item.ChildNodes [1].InnerText;
			_m.howMuch = item.ChildNodes [2].InnerText;
			_m.property = item.ChildNodes [3].InnerText;
			_m.allInfo = item.ChildNodes [4].InnerText;
			MyPageList.mp.Add (_m);//将对象添加到一个List中。
                }
		doc.Save (xmlFielPath);
	}
<?xml version="1.0" encoding="UTF-8"?>
<Pages>
  <page>
    <ID>b</ID>
    <equName>bluePotion01</equName>
    <howMuch>99</howMuch>
    <property>AddBlue 500/s,Total 5s</property>
    <Allinfo>this is a magic bottle,it can support the mage release skills, and againt the</Allinfo>
  </page>
  <page>
    <ID>b</ID>
    <equName>bluePotion02</equName>
    <howMuch>88</howMuch>
    <property>AddBlue 1500/s,Total 5s</property>
    <Allinfo>this is a magic bottle,it can support the mage release skills, and againt the</Allinfo>
  </page>
  <page>
    <ID>c</ID>
    <equName>cloth01</equName>
    <howMuch>1</howMuch>
    <property>Defense 150d</property>
    <Allinfo>Protect your chest!</Allinfo>
  </page>
  <page>
    <ID>c</ID>
    <equName>cloth02</equName>
    <howMuch>1</howMuch>
    <property>Defense 350d</property>
    <Allinfo>Protect your chest!</Allinfo>
  </page>
  <page>
    <ID>h</ID>
    <equName>hat01</equName>
    <howMuch>1</howMuch>
    <property>Defense 120d</property>
    <Allinfo>Protect your head!</Allinfo>
  </page>
  <page>
    <ID>h</ID>
    <equName>hat02</equName>
    <howMuch>1</howMuch>
    <property>Defense 150d</property>
    <Allinfo>Protect your head!</Allinfo>
  </page>
  <page>
    <ID>t</ID>
    <equName>trousers01</equName>
    <howMuch>1</howMuch>
    <property>Defense 100d</property>
    <Allinfo>Protect your thighs!</Allinfo>
  </page>
  <page>
    <ID>t</ID>
    <equName>trousers02</equName>
    <howMuch>1</howMuch>
    <property>Defense 150d</property>
    <Allinfo>Protect your thighs!</Allinfo>
  </page>
  <page>
    <ID>w</ID>
    <equName>weapon01</equName>
    <howMuch>1</howMuch>
    <property>Attack 250A</property>
    <Allinfo>It can help you to kill another player in game!</Allinfo>
  </page>
  <page>
    <ID>r</ID>
    <equName>redPotion02</equName>
    <howMuch>87</howMuch>
    <property>trest 700/s,6s</property>
    <Allinfo>Treat your life!</Allinfo>
  </page>
</Pages>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值