微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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;

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念
xml文件介绍及使用
xml编程(一)-xml语法
XML文件结构和基本语法
第2章 包装类