uinty中对Xml文件的操作
生活随笔
收集整理的這篇文章主要介紹了
uinty中对Xml文件的操作
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
???? ? 最近學習unity感覺到了瓶頸,然后就回顧一下學到的知識,將它們整理下來;
????????unity中用戶的登錄與注冊需要將用戶名和密碼保存起來的,方法有很多,如將其保存到數據庫,或用JSON保存到磁盤,這里就簡單說說用Xml對其進行讀寫操作吧;
????????
????????首先,對Xml進行操作需要的命名空間是 using system.IO;
????????????????????????????????????????????????????????????????????using system.Xml;
????????先附上一段代碼,再添加注釋解釋吧;
????????
????????
using?system.IO; using?system.Xml;void?Start() {private?string?path_Xml=Application.dataPath+"/User.xml";??//保存文件的路徑;if(!File.Exists(path_Xml))???????????????????????????????//判斷文件是否存在,如果不存在就創建Xml文件;{XmlDocument?xmlDoc=new?XmlDocument();??XmlElement?root=xmlDoc.CreateElement("Root");???????//創建根結點;XmlDoc.AppendChild(root);???????????????????????????//將根節點綁定到Xml對象上;XmlElement?user=xmlDoc.CreateElement("User");???????//再創建一個user結點;user.SetAttribute("user_name","userOne");???????????//將userOne保存到user結點中;user.SetAttribute("user_pass","1242434");user.setAttribute("user_address","hainan");root.AppendChild(user);?????????????????????????????//將user結點綁定到root根節點上;xmlDoc.Save(path_Xml);??????????????????????????????//用Save方法將信息保存到User.xml中;}}以上就是對xml文件的寫操作,那么如何進行讀取呢?同樣還是先創建XmlDocument對象;XmlDocument?xmlDoc=new?XmlDocument() xmlDoc.Load(path_Xml);????????????????//加載文件; XmlNodeList?nodeList=xmlDoc.SelectSingleNode("Root").ChildNodes;??????//取得Root結點下的所有子節點; foreach(XmlElement?xe?in?nodeList) {if(xe.GetAttribute("user_name")=="")?????????????????????????//用XmlElement對象的GetAttribute方法取得結點;{} }總結:主要就是XmlDocument對象的CreateElement(),AppendChild(),Save(),Load(),SelectSingleNode()方法以及XmlElement對象的AppendChild(),SetAttribute(),GetAttribute()等方法;????
轉載于:https://blog.51cto.com/cjboking/1784368
總結
以上是生活随笔為你收集整理的uinty中对Xml文件的操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 第二阶段—个人工作总结08
- 下一篇: 如何设置Windows server 2