網站首頁 編程語言 正文
通過XML文件保存賬號密碼,存儲到本地,不連接數據庫的簡易登錄系統。
1.創建一個XML文件,設置一個初始的賬號密碼。
public void Creat() { localPath = UnityEngine.Application.streamingAssetsPath + "/" + "MyXML.xml"; if (!File.Exists(localPath)) { XmlDocument xml = new XmlDocument(); XmlDeclaration xmldecl = xml.CreateXmlDeclaration("1.0", "UTF-8", "");//設置xml文件編碼格式為UTF-8 XmlElement root = xml.CreateElement("Data");//創建根節點 XmlElement info = xml.CreateElement("Info");//創建子節點 info.SetAttribute("賬號", "admin");//創建子節點屬性名和屬性值 info.SetAttribute("密碼", "123123"); root.AppendChild(info);//將子節點按照創建順序,添加到xml xml.AppendChild(root); xml.Save(localPath);//保存xml到路徑位置 Debug.Log("創建XML成功!"); } }
2.讀取XML文件,將賬號密碼存儲到列表數組中。每次讀取的時候需要把index的值設置成0,重新錄入賬號密碼,不然會出現一個賬號密碼保存兩次的問題。
void ReadXML() { localPath = UnityEngine.Application.streamingAssetsPath + "/" + "MyXML.xml"; if (File.Exists(localPath)) { XmlDocument xml = new XmlDocument(); xml.Load(localPath);//加載xml文件 XmlNodeList nodeList = xml.SelectSingleNode("Data").ChildNodes; MimaZu = nodeList.Count; foreach (XmlElement xe in nodeList) {//遍歷所以子節點 if (xe.Name == "Info") { Debug.Log(xe.GetAttribute("賬號"));//獲取Name屬性值 Debug.Log(xe.GetAttribute("密碼")); messages[index].vs.Add(xe.GetAttribute("賬號")); messages[index].vs.Add(xe.GetAttribute("密碼")); index += 1; //messages.Add(new string[] { xe.GetAttribute("賬號"), xe.GetAttribute("密碼") }); //Debug.Log(vs); } } Debug.Log("讀取XML成功!" + xml.OuterXml); } }
3.列表數組的創建
public Message[] messages; //這個放在外面 [System.Serializable] public class Message { public Listvs = new List (); }
4.添加賬號密碼
public void AddXML(string zhanghao, string mima) { localPath = UnityEngine.Application.streamingAssetsPath + "/" + "MyXML.xml"; if (File.Exists(localPath)) { XmlDocument xml = new XmlDocument(); xml.Load(localPath);//加載xml文件 XmlNode root = xml.SelectSingleNode("Data");//獲取根節點 XmlElement info = xml.CreateElement("Info");//創建新的子節點 info.SetAttribute("賬號", zhanghao);//創建新子節點屬性名和屬性值 info.SetAttribute("密碼", mima); root.AppendChild(info);//將子節點按照創建順序,添加到xml xml.AppendChild(root); xml.Save(localPath);//保存xml到路徑位置 Debug.Log("添加XML成功!"); } }
5.判斷賬號密碼是否正確,登錄的時候判斷IsLogin的值,如果IsLogin的值為false就是登錄失敗,如果值為True就是登錄成功
public bool IsLogin; public Message[] messages; ////// 賬號密碼個數 /// ////// 判斷賬號密碼是否正確 /// public void Login() { for (int i = 0; i < MimaZu; i++) { int index = i; if (ZhangHao.text == messages[index].vs[0] && Mima.text == messages[index].vs[1]) { if (messages[index].vs[0] == null) return; if (messages[index].vs[1] == null) return; IsLogin = true; return; } else if (ZhangHao.text != messages[index].vs[0] && Mima.text != messages[index].vs[1]) { IsLogin = false; } } }
原文鏈接:https://blog.csdn.net/Chinesettt/article/details/123543207
相關推薦
- 2022-03-06 Linux之Centos8創建CA證書教程_Linux
- 2022-05-12 Kotlin any/none/all 函數
- 2021-12-11 Linux環境下查看日志文件命令詳解_Linux
- 2023-02-01 深入分析C語言存儲類型與用戶空間內部分布_C 語言
- 2022-06-17 使用Python解決常見格式圖像讀取nii,dicom,mhd_python
- 2022-05-10 .NET實現異步編程async和await_實用技巧
- 2023-11-21 conda創建python虛擬環境常用指令和流程
- 2022-10-03 python接口自動化使用requests庫發送http請求_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支