unity对Xml文件进行读取



<?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; } } 
           
          
        
       
       
      
      
     
     
    
    

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值