unity c#读取xml
来源:互联网 发布:我的世界pe我是僵尸js 编辑:程序博客网 时间:2024/05/21 16:56
using System.Collections;
using System.Xml;
using System.IO;
public class ReadXml : MonoBehaviour {
private string[] myNames;
// Use this for initialization
void Start () {
LoadMyXML();
}
// Update is called once per frame
void Update () {
}
private void LoadMyXML()
{
//xml存放路径
string filePath = Application.dataPath + @"/testxml.xml";
Debug.Log ("filePath=" + filePath);
if (File.Exists(filePath))
{
//声明个xml文档
XmlDocument xmlDocument = new XmlDocument();
settings.IgnoreComments = true;//忽略文档里面的注释
//获取名称为myRoot的根节点
XmlNodeList xmlNodeList = xmlDocument.SelectSingleNode("myRoot").ChildNodes;
//Debug.Log(xmlNodeList.Count);
//遍历子节点
Debug.Log("readxml");
foreach (XmlElement xmlElement1 in xmlNodeList)
{
if (xmlElement1.Name == "myChild")
{
int index = 0;
myNames = new string[xmlElement1.ChildNodes.Count];
//Debug.Log(xmlElement1.ChildNodes.Count);
foreach (XmlElement xmlElement2 in xmlElement1.ChildNodes)
{
myNames[index] = xmlElement2.InnerText;
Debug.Log(myNames[index]);
index++;
}
}
}
}
}
}
xml文件
<?xml version="1.0" encoding="UTF-8"?>
<myRoot>
<myChild>
<myName>张三</myName>
<myName>李四</myName>
<myName>王老五</myName>
<myName>刘邦</myName>
<myName>项羽</myName>
<myName>秦始皇</myName>
<!--记录书本的信息-->
</myChild>
</myRoot>
xml文件保存路径
string filePath;
#if UNITY_STANDALONE_WIN && UNITY_EDITOR
filePath=Application.dataPath + @"/UserMessageXml.xml";
#elif UNITY_IOS && UNITY_ANDROID
filePath=Application.persistentDataPath+"/UserMessageXml.xml";
#endif
Debug.Log ("path=" + filePath);
XmlDocument xmlDoc = new XmlDocument();
XmlElement root = xmlDoc.CreateElement("transforms");
xmlDoc.AppendChild(root);
xmlDoc.Save(filepath);
移动端需要一开始就创建一个xml,不能保存到Resource中的xml文件。
xml文件读取路径
//从Resource中加载; 一般用于移动端
TextAsset t = Resources.Load("Init") as TextAsset ;
xmlDocument.Load(XmlReader.Create(new StringReader(t.text),settings));
//从发布文件中的XXXXX_Data中读取,需要手动添加xml文件 一般用于PC
filePath=Application.dataPath+"/Init1.xml";
xmlDocument.Load(XmlReader.Create(filePath,settings));
- unity c#读取xml
- Unity 读取xml 文件
- Unity 读取xml 文件
- unity中xml读取
- unity 读取xml 信息
- Unity读取XML定制。
- unity的XML读取
- Unity 读取XML文件
- unity读取xml
- Unity读取xml(android)
- unity中读取xml文件
- Unity 中读取XML文件
- unity中WWW读取xml
- 【Unity&XML】读取XML文件内容
- C# 读取XML文档
- C#学习:读取XML
- C#读取XML文档
- C#读取XML配置文件
- Oracle中Hint深入理解
- VS2012只生成dll不生成lib的解决方案
- 51单片机EA=1的问题
- 前置增强的实现
- Java中的阻塞和非阻塞IO包各自的优劣思考
- unity c#读取xml
- java解析xml的几种方式哪种最好?
- 使用双线插值算法缩放位图图像
- win8、server 2012 清除winsxs文件夹
- 采样中的数据类型转换
- STL remove和remove_if
- python关于__all__的定义
- JM86 中 POC 的计算方法
- TreeSet与排序_实践应用,TreeSet显示某商品的若干信息