纳金网
标题:
unity c#读取xml
[打印本页]
作者:
王者再临
时间:
2014-12-31 23:00
标题:
unity c#读取xml
using UnityEngine;
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();
XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreComments = true;//忽略文档里面的注释
xmlDocument.Load(XmlReader.Create(filePath,settings));
//获取名称为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>
复制代码
作者:
tianhett
时间:
2014-12-31 23:24
涨姿势了。。。。。。。。
作者:
KISS2015
时间:
2015-1-2 19:22
不错的C#代码
作者:
我不再年轻
时间:
2015-1-3 11:08
不错, 学习了!
欢迎光临 纳金网 (http://go.narkii.com/club/)
Powered by Discuz! X2.5