【XML】XML在项目中使用

来源:互联网 发布:mysql datediff函数 编辑:程序博客网 时间:2024/06/06 19:31

XML 读取 目前 常用的 三种 

  1. XmlDocument
  2. XmlTextReader
  3. Linq to Xml



using UnityEngine;using UnityEngine.UI;using System.Collections;using WhtGame;using cn.sharesdk.unity3d;using EasyAR;using System.IO;using DG.Tweening;using System.Xml;using System.Collections.Generic;using System;public class UIUpload1 : UIBaseWindow{public UnityEngine.UI.RawImage ui_BG;public UnityEngine.UI.RawImage ui_BG_1;[HideInInspector]public bool isPicLoop;private string filename;private string temp;private int index = 0;private float waitTime = 2.2f;private Texture temp_Texture;private int int_FileChild = 0;DirectoryInfo dirInfo;private string default_Path;public Upload_UI[] uipload;public GameObject[] ui_Start;private string xml_Path;private int default_Number = 0;private int active_Number = 0;private int alpha_Num = 0;private int interable_Num = 0;private bool isHasID;private XmlElement xe_All;void Awake (){# if UNITY_EDITOR//mySprite_Path = "file://" + Application.streamingAssetsPath + "/";//temp = "/StreamingAssets/ScreenShot/";filename = "file:///" + Application.persistentDataPath + "/ScreenShot/";default_Path = Application.persistentDataPath + "/ScreenShot/";# else#if UNITY_ANDROIDtemp = "/ScreenShot/";filename = "file://" + Application.persistentDataPath + temp;default_Path = Application.persistentDataPath + temp;#endif#if UNITY_IPHONEtemp = "/Raw/ScreenShot/";filename = "file://"+ Application.temporaryCachePath + temp;default_Path = Application.temporaryCachePath + temp;#endif#if UNITY_STANDALONE_WINfilename = "Screenshot.png";#endif#endif//XML# if UNITY_EDITORxml_Path = Application.persistentDataPath + @"/my.xml";# else#if UNITY_ANDROIDxml_Path = "file://" + Application.persistentDataPath + @"/my.xml";#endif#if UNITY_IPHONExml_Path = "file://"+ Application.temporaryCachePath + @"/my.xml";#endif#endifBtnOnClick ("UIUpload1_UPdataBtn", this.OnClick);BtnOnClick ("UIUpload1_MsgBtn", this.OnClick);BtnOnClick ("UIUpload1_ScanBtn", this.OnClick);BtnOnClick ("UIUpload1_PlayBtn", this.OnClick);BtnOnClick ("UIUpload1_WatchBtn", this.OnClick);}void Update (){if (isPicLoop) {if (int_FileChild != 0) {waitTime += Time.deltaTime;if (waitTime > 2.2f) {waitTime = 0;index++;if (index >= int_FileChild + 1) {index = 1;}StartCoroutine (LoadPicture (index));}}}}IEnumerator LoadPicture (int index){//WWW www = new WWW(mySprite_Path + myDataAll[index].Image);WWW www = new WWW (filename + index + ".png");//Debug.Log ("加载图片的路径" + www.url);yield return www;if (www.error != null) {Debug.Log (www.error);} else if (www.isDone) {temp_Texture = www.texture;ChangeSprite ();}}public void ChangeSprite (){ui_BG.gameObject.SetActive (false);ui_BG_1.texture = temp_Texture;}public int GetFileChild (){dirInfo = new DirectoryInfo (default_Path);if (dirInfo.Exists == false) {return int_FileChild = 0;}foreach (FileInfo item in dirInfo.GetFiles("*.png")) {//print (item.Extension);int_FileChild++;}return int_FileChild;}public override void OnUIShow (){isPicLoop = true;int_FileChild = 0;GetFileChild ();LoadXML ();}private void OnClick (GameObject go){isPicLoop = false;//在这里监听按钮的点击事件//提交按钮if (go.name == "UIUpload1_UPdataBtn") {//UIManager.GetInstance ().ShowWindow (WindowID.WindowID_ARCreat);CreateXML (1);UIManager.GetInstance ().ShowWindow (WindowID.WindowID_Upload2);UIManager.GetInstance ().HideWindow (WindowID.WindowID_Upload1);Debug.Log ("Show UIUpload2");} /*else if (go.name == "UploadBackButton") {UIManager.GetInstance ().HideWindow (WindowID.WindowID_Upload1);UIManager.GetInstance ().ShowWindow (WindowID.WindowID_AR);UIManager.GetInstance ().ShowWindow (WindowID.WindowID_UIMainMenu);}*/if (go.name == "UIUpload1_MsgBtn") {//UIManager.GetInstance ().ShowWindow (WindowID.WindowID_UISideLine);UIManager.GetInstance ().ShowWindow (WindowID.WindowID_AR);UIManager.GetInstance ().HideWindow (WindowID.WindowID_Upload1);UIManager.GetInstance ().ShowWindow (WindowID.WindowID_UIMainMenu);Debug.Log ("Show UIUpload2");}if (go.name == "UIUpload1_ScanBtn") {ARBuilder.Instance.CameraDeviceBehaviours [0].OpenAndStart ();UIManager.GetInstance ().HideWindow (WindowID.WindowID_Home);UIManager.GetInstance ().HideWindow (WindowID.WindowID_AR);UIManager.GetInstance ().HideWindow (WindowID.WindowID_Upload1);UIManager.GetInstance ().HideWindow (WindowID.WindowID_UIMainMenu);UIManager.GetInstance ().ShowWindow (WindowID.WindowID_UIScreen);}//play 按钮if (go.name == "UIUpload1_PlayBtn") {CreateXML (0);}//观看获奖作品动画if (go.name == "UIUpload1_WatchBtn") {CreateXML (2);}}//显示可提交 等 UI模块public void CreateXML (int index){uipload [index].text_OBJ [0].SetActive (false);uipload [index].text_OBJ [1].SetActive (true);//DOTween.ToAlpha (() => uipload [index].image_UI.color, x => uipload [index].image_UI.color = x, 1, 0f);uipload [index].image_UI.color = new Color (1, 1, 1, 1);uipload [index].btn_OBJ.interactable = true;if (!File.Exists (xml_Path)) {//创建XML文档XmlDocument xmldoc = new XmlDocument ();//创建rootXmlElement root = xmldoc.CreateElement ("State");//创建下一层节点XmlElement attribute = xmldoc.CreateElement ("Attribute");attribute.SetAttribute ("ID", UIManager.GetInstance ().getId.ToString ());//创建级别XmlElement Level = xmldoc.CreateElement ("Level");Level.SetAttribute ("Number", "0");JudgeT_or_F (0);XmlElement text_Default = xmldoc.CreateElement ("defaultText");text_Default.InnerText = default_Number.ToString ();XmlElement text_Active = xmldoc.CreateElement ("activeText");text_Active.InnerText = active_Number.ToString ();XmlElement alpha_Number = xmldoc.CreateElement ("alpha_Number");alpha_Number.InnerText = alpha_Num.ToString ();XmlElement interable_Number = xmldoc.CreateElement ("interable_Number");interable_Number.InnerText = interable_Num.ToString ();XmlElement Level_1 = xmldoc.CreateElement ("Level");Level_1.SetAttribute ("Number", "1");JudgeT_or_F (1);XmlElement text_Default_1 = xmldoc.CreateElement ("defaultText");text_Default_1.InnerText = default_Number.ToString ();XmlElement text_Active_1 = xmldoc.CreateElement ("activeText");text_Active_1.InnerText = active_Number.ToString ();XmlElement alpha_Number_1 = xmldoc.CreateElement ("alpha_Number");alpha_Number_1.InnerText = alpha_Num.ToString ();XmlElement interable_Number_1 = xmldoc.CreateElement ("interable_Number");interable_Number_1.InnerText = interable_Num.ToString ();XmlElement Level_2 = xmldoc.CreateElement ("Level");Level_2.SetAttribute ("Number", "2");JudgeT_or_F (2);XmlElement text_Default_2 = xmldoc.CreateElement ("defaultText");text_Default_2.InnerText = default_Number.ToString ();XmlElement text_Active_2 = xmldoc.CreateElement ("activeText");text_Active_2.InnerText = active_Number.ToString ();XmlElement alpha_Number_2 = xmldoc.CreateElement ("alpha_Number");alpha_Number_2.InnerText = alpha_Num.ToString ();XmlElement interable_Number_2 = xmldoc.CreateElement ("interable_Number");interable_Number_2.InnerText = interable_Num.ToString ();Level.AppendChild (text_Default);Level.AppendChild (text_Active);Level.AppendChild (alpha_Number);Level.AppendChild (interable_Number);Level_1.AppendChild (text_Default_1);Level_1.AppendChild (text_Active_1);Level_1.AppendChild (alpha_Number_1);Level_1.AppendChild (interable_Number_1);Level_2.AppendChild (text_Default_2);Level_2.AppendChild (text_Active_2);Level_2.AppendChild (alpha_Number_2);Level_2.AppendChild (interable_Number_2);attribute.AppendChild (Level);attribute.AppendChild (Level_1);attribute.AppendChild (Level_2);root.AppendChild (attribute);xmldoc.AppendChild (root);xmldoc.Save (xml_Path);Debug.Log ("xml_Path" + xml_Path);Debug.Log ("createXml OK!");} else {//更新参数XmlDocument xmldoc = new XmlDocument ();xmldoc.Load (xml_Path);XmlNodeList nodeList = xmldoc.SelectSingleNode ("State").ChildNodes;foreach (XmlElement item in nodeList) {int b = UIManager.GetInstance ().getId;if (item.GetAttribute ("ID") == UIManager.GetInstance ().getId.ToString ()) {isHasID = true;xe_All = item;break;} else {isHasID = false;}}if (isHasID) {foreach (XmlElement xe in xe_All) {if (xe.GetAttribute ("Number") == index.ToString ()) {JudgeT_or_F (index);foreach (XmlElement x1 in xe) {if (x1.Name == "defaultText") {x1.InnerText = default_Number.ToString ();continue;}if (x1.Name == "activeText") {x1.InnerText = active_Number.ToString ();continue;}if (x1.Name == "alpha_Number") {x1.InnerText = alpha_Num.ToString ();continue;}if (x1.Name == "interable_Number") {x1.InnerText = interable_Num.ToString ();continue;}}}}xmldoc.Save (xml_Path);} else {Debug.Log ("创建新的ID!!!!!!!!!!!!!!!!!!!!!");//不同item 创建新的数据XmlNode root = xmldoc.SelectSingleNode ("State");XmlElement attribute = xmldoc.CreateElement ("Attribute");attribute.SetAttribute ("ID", UIManager.GetInstance ().getId.ToString ());//创建级别XmlElement Level = xmldoc.CreateElement ("Level");Level.SetAttribute ("Number", "0");JudgeT_or_F (0);XmlElement text_Default = xmldoc.CreateElement ("defaultText");text_Default.InnerText = default_Number.ToString ();XmlElement text_Active = xmldoc.CreateElement ("activeText");text_Active.InnerText = active_Number.ToString ();XmlElement alpha_Number = xmldoc.CreateElement ("alpha_Number");alpha_Number.InnerText = alpha_Num.ToString ();XmlElement interable_Number = xmldoc.CreateElement ("interable_Number");interable_Number.InnerText = interable_Num.ToString ();XmlElement Level_1 = xmldoc.CreateElement ("Level");Level_1.SetAttribute ("Number", "1");JudgeT_or_F (1);XmlElement text_Default_1 = xmldoc.CreateElement ("defaultText");text_Default_1.InnerText = default_Number.ToString ();XmlElement text_Active_1 = xmldoc.CreateElement ("activeText");text_Active_1.InnerText = active_Number.ToString ();XmlElement alpha_Number_1 = xmldoc.CreateElement ("alpha_Number");alpha_Number_1.InnerText = alpha_Num.ToString ();XmlElement interable_Number_1 = xmldoc.CreateElement ("interable_Number");interable_Number_1.InnerText = interable_Num.ToString ();XmlElement Level_2 = xmldoc.CreateElement ("Level");Level_2.SetAttribute ("Number", "2");JudgeT_or_F (2);XmlElement text_Default_2 = xmldoc.CreateElement ("defaultText");text_Default_2.InnerText = default_Number.ToString ();XmlElement text_Active_2 = xmldoc.CreateElement ("activeText");text_Active_2.InnerText = active_Number.ToString ();XmlElement alpha_Number_2 = xmldoc.CreateElement ("alpha_Number");alpha_Number_2.InnerText = alpha_Num.ToString ();XmlElement interable_Number_2 = xmldoc.CreateElement ("interable_Number");interable_Number_2.InnerText = interable_Num.ToString ();Level.AppendChild (text_Default);Level.AppendChild (text_Active);Level.AppendChild (alpha_Number);Level.AppendChild (interable_Number);Level_1.AppendChild (text_Default_1);Level_1.AppendChild (text_Active_1);Level_1.AppendChild (alpha_Number_1);Level_1.AppendChild (interable_Number_1);Level_2.AppendChild (text_Default_2);Level_2.AppendChild (text_Active_2);Level_2.AppendChild (alpha_Number_2);Level_2.AppendChild (interable_Number_2);attribute.AppendChild (Level);attribute.AppendChild (Level_1);attribute.AppendChild (Level_2);root.AppendChild (attribute);xmldoc.AppendChild (root);xmldoc.Save (xml_Path);}}InitNumber ();}public void JudgeT_or_F (int index){if (!uipload [index].text_OBJ [0].activeInHierarchy) {default_Number = 0;} else {default_Number = 1;}if (!uipload [index].text_OBJ [1].activeInHierarchy) {active_Number = 0;} else {active_Number = 1;}if (uipload [index].image_UI.color.a == 0f) {alpha_Num = 0;} else {alpha_Num = 1;}if (uipload [index].btn_OBJ.interactable == false) {interable_Num = 0;} else {interable_Num = 1;}}public void LoadJudge (int default_Num, int active_Num, int alpha_Num, int interable_Num, int index){if (default_Num == 0) {uipload [index].text_OBJ [0].SetActive (false);} else {uipload [index].text_OBJ [0].SetActive (true);}if (active_Num == 0) {uipload [index].text_OBJ [1].SetActive (false);} else {uipload [index].text_OBJ [1].SetActive (true);}if (alpha_Num == 0) {uipload [index].image_UI.color = new Color (1, 1, 1, 0);} else {uipload [index].image_UI.color = new Color (1, 1, 1, 1);}if (interable_Num == 0) {uipload [index].btn_OBJ.interactable = false;} else {uipload [index].btn_OBJ.interactable = true;}}public void InitNumber (){default_Number = 1;active_Number = 0;alpha_Num = 0;interable_Num = 0;}public void LoadXML (){if (File.Exists (xml_Path)) {XmlDocument xmldoc = new XmlDocument ();xmldoc.Load (xml_Path);XmlNodeList nodeList = xmldoc.SelectSingleNode ("State").ChildNodes;foreach (XmlElement item in nodeList) {//如果存在则读取if (item.GetAttribute ("ID") == UIManager.GetInstance ().getId.ToString ()) {isHasID = true;xe_All = item;break;} else {isHasID = false;}}if (isHasID) {int a = UIManager.GetInstance ().getId;foreach (XmlElement xe in xe_All) {for (int i = 0; i < 3; i++) {if (xe.GetAttribute ("Number") == i.ToString ()) {foreach (XmlElement x1 in xe) {if (x1.Name == "defaultText") {default_Number = Convert.ToInt32 (x1.InnerText);}if (x1.Name == "activeText") {active_Number = Convert.ToInt32 (x1.InnerText);}if (x1.Name == "alpha_Number") {alpha_Num = Convert.ToInt32 (x1.InnerText);}if (x1.Name == "interable_Number") {interable_Num = Convert.ToInt32 (x1.InnerText);}}LoadJudge (default_Number, active_Number, alpha_Num, interable_Num, i);}}}} else {for (int j = 0; j < 3; j++) {uipload [j].text_OBJ [0].SetActive (true);uipload [j].text_OBJ [1].SetActive (false);uipload [j].image_UI.color = new Color (1, 1, 1, 0);uipload [j].btn_OBJ.interactable = false;}}}}}[System.Serializable]public class Upload_UI{public GameObject[] text_OBJ;public UnityEngine.UI.Image image_UI;public Button btn_OBJ;}

XMLDocument 读取

using UnityEngine;using System.Collections;using UnityEngine.UI;using System.Xml;public class ParseXml : MonoBehaviour{public GameObject btn_LoadXML;private XmlElement xe_All;void Start (){Button btn = btn_LoadXML.GetComponent<Button> ();btn.onClick.AddListener (delegate {LoadXml ();});}public void LoadXml (){UnityEditor.AssetDatabase.Refresh ();XmlDocument xml = new XmlDocument ();xml.Load (Application.dataPath + @"/my.xml");XmlNodeList nodeList = xml.SelectSingleNode ("State").ChildNodes;foreach (XmlElement item in nodeList) {  xe_All = item;foreach (XmlElement xe in xe_All) {  if (xe.GetAttribute ("Number") == 0.ToString ()) {  foreach (XmlElement x1 in xe) {  Debug.Log (xe.ChildNodes.Count);if (x1.Name == "defaultText") {  Debug.Log (x1.InnerText); continue;  }  if (x1.Name == "activeText") {  Debug.Log (x1.InnerText); continue;  }  if (x1.Name == "alpha_Number") {  Debug.Log (x1.InnerText); continue;  }  if (x1.Name == "interable_Number") {  Debug.Log (x1.InnerText); continue;  }  }   }  }}}}


2 0
原创粉丝点击