xml 读取

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using UnityEditor;
using UnityEditorInternal;
using System;
using System.Reflection;
using System.Text;
public class ReadXML : Editor {
	[MenuItem("Tools/程序/读取XML # a s")]
	static void LoadSplitXML()
	{
		List<LayerData> allData=new List<LayerData>();

		string path =Application.streamingAssetsPath + "/" + "test_apla.xml";
		XmlDocument xmlDoc = new XmlDocument();
		xmlDoc.Load(path);
		XmlElement rootElem = xmlDoc.DocumentElement;
		//Debug.Log(rootElem.Name);
		for (int i = 0; i < rootElem.ChildNodes.Count; ++i){
			XmlNode textureLayer=rootElem.ChildNodes[0];
			//Debug.Log(textureLayer.Name);
			for(int j=0;j<textureLayer.ChildNodes.Count;j++){
				XmlNode layerChild=textureLayer.ChildNodes[j];
				Debug.Log(layerChild.Name);
				LayerData data=new LayerData();
				for(int m=0;m<layerChild.ChildNodes.Count;m++){
					Debug.Log(layerChild.ChildNodes[m].Name);
					if(layerChild.ChildNodes[m].Name=="name")data.name=layerChild.ChildNodes[m].InnerText;
					if(layerChild.ChildNodes[m].Name=="Path")data.Path=layerChild.ChildNodes[m].InnerText;
					if(layerChild.ChildNodes[m].Name=="Posx")data.Posx=float.Parse(layerChild.ChildNodes[m].InnerText);
					if(layerChild.ChildNodes[m].Name=="Posy")data.Posy=float.Parse(layerChild.ChildNodes[m].InnerText);
				}
				allData.Add(data);
           
			}
		}

		for(int i=0;i<allData.Count;i++){
			Debug.Log(allData[i].Path);
			Debug.Log(allData[i].name);
			Debug.Log(allData[i].Posx);
			Debug.Log(allData[i].Posy);
		}
	}
}


public class LayerData{
	public string name;
	public string Path;
	public float Posx;
	public float Posy;
}


下面是XML文件

<?xml version="1.0" encoding="utf-8"?>
<PSScene>
<Layers>
	<Layer>
		<name>Layer 1</name>
		<Path>Group 5/Group 4/Group 3/Group 2/Group 1/Layer 1</Path>
		<Posx>-33</Posx>
		<Posy>38</Posy>
		</Layer>
	<Layer>
		<name>my channel</name>
		<Path>Group 5/Group 4/Group 3/Group 2/Group 1/my channel</Path>
		<Posx>44</Posx>
		<Posy>41.5</Posy>
		</Layer>
	<Layer>
		<name>my layer</name>
		<Path>Group 5/Group 4/Group 3/Group 2/Group 1/my layer</Path>
		<Posx>0</Posx>
		<Posy>1.5</Posy>
		</Layer>
	<Layer>
		<name>my channel copy</name>
		<Path>my channel copy</Path>
		<Posx>-43</Posx>
		<Posy>-51.5</Posy>
	</Layer>
	<Layer>
		<name>my layer copy</name>
		<Path>my layer copy</Path>
		<Posx>46</Posx>
		<Posy>-51.5</Posy>
	</Layer>
</Layers>
</PSScene>

如果节点处为属性则代码为这样

XmlNode actionNode = list.ChildNodes[i];
lieBiao.name=actionNode.Name;
XmlAttribute guodu = actionNode.Attributes["guodu"];
lieBiao.guodu=int.Parse(guodu.InnerText);
Debug.Log(guodu.InnerText);
XmlAttribute miaoshu = actionNode.Attributes["miaoshu"];
lieBiao.miaoshu=miaoshu.InnerText;



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值