<?xml version="1.0" standalone="yes"?>
<RECORDS>
<RECORD Id="1" Title="年满18周岁才可以成为志愿者?" Answer="0"/>
</RECORDS>
xml文件格式
/**
*Copyright(C) 2017 by MMHD
*All rights reserved.
*FileName: XmlMgr.cs
*Author: Joel
*Version: 1.0
*UnityVersion:5.6.1f1
*Date: 2017-09-04
*Description:
*History:
*/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Xml;
public class XmlMgr : MonoBehaviour {
private static XmlMgr mInstance = null;
public static Dictionary
QAk = null;
public void Awake()
{
QAk = LoadXml();
//Debug.Log(QAk[1].title);
}
public static XmlMgr GetInstance()
{
if (mInstance == null)
{
mInstance = new GameObject("XmlMgr").AddComponent
();
}
return mInstance;
}
private string path1 = "DaTi";
private Dictionary
mNeedleDic = null;
private Dictionary
mDishDic = null;
///
读取xml
public Dictionary
LoadXml() { string path = Application.streamingAssetsPath + @"\" + path1 + ".xml"; XmlReader reader = new XmlTextReader(path); Dictionary
dic = new Dictionary
(); while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element) { if (reader.LocalName == "RECORD") { QAData data = new QAData(); for (int i = 0; i < reader.AttributeCount; i++) { reader.MoveToAttribute(i); if (reader.Name == "Id") data.id = int.Parse(reader.Value); else if (reader.Name == "Title") data.title = reader.Value; else if (reader.Name == "Answer") data.answer = int.Parse(reader.Value); }; if (!dic.ContainsKey(data.id)) dic.Add(data.id, data); } } } return dic; } //public QAData GetTitleInfo(int id) //{ // QAData data; // if (mNeedleDic == null) mNeedleDic = LoadXml(); // data = mNeedleDic[id]; // Debug.Log(111111111); // return data; //} //public float[] GetAnswerAll() //{ // if (mNeedleDic == null) mNeedleDic = LoadXml(); // float[] pro = new float[mNeedleDic.Count]; // int i = 0; // foreach (int key in mNeedleDic.Keys) // { // pro[i] = mNeedleDic[key].answer; // i++; // } // Debug.Log(pro); // return pro; //} public class QAData { public int id; public string title; public int answer; } }