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;

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念