xml的应用(账号密码的注册)
这个是打包安卓设置路径
// DataPath = Application.persistentDataPath + @"\yangfufeng.xml";
///
/// 注册账号和密码
///
public void LoginAccountNumberAndPassWord()
{
//注册账号是否重号的判断
bool isExit = false;
//定义路径(当然了,如果打包成ext,那么xml就要放到Resources文件夹下,不然打包的时候不会打包出去)
string path = Application.dataPath + “/Xml/xml_player.xml”;
//如果账号和密码有一个为空,则不能创建。
if (accountNumber.text.Trim().ToString() !="" && passWord.text.Trim().ToString() != “”)
{
//判断该路径下的文件是否存在
if (!File.Exists(path))
{
XmlDocument xmlDoc = new XmlDocument();
//根节点创建
XmlElement root = xmlDoc.CreateElement("Sign");
XmlElement player = xmlDoc.CreateElement("player");
player.SetAttribute("name", accountNumber.text);
player.SetAttribute("passWord", passWord.text);
xmlDoc.AppendChild(root);
root.AppendChild(player);
//保存xml文件(保存到的路径)
xmlDoc.Save(path);
}else
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(path);
XmlNodeList nodelist = xmlDoc.SelectSingleNode("Sign").ChildNodes;
foreach (XmlElement xe in nodelist)
{
if (accountNumber.text.Trim().ToString() == xe.GetAttribute("name"))
{
return;
}
}
if(!isExit)
{
XmlNode root = xmlDoc.SelectSingleNode("Sign");
XmlElement player = xmlDoc.CreateElement("player");
player.SetAttribute("name", accountNumber.text);
player.SetAttribute("passWord", passWord.text);
root.AppendChild(player);
//保存xml文件(保存到的路径)
xmlDoc.Save(path);
}
}
}
}
///
/// 知道账号和密码,然后修改账号和密码
///
public void AlterAccountNumberAndPassWord()
{
//定义路径
string path = Application.dataPath + “/Xml/xml_player.xml”;
//判断要修改的xml文件是否存在
if(File.Exists(path))
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(path);
XmlNodeList nodeList = xmlDoc.SelectSingleNode(“Sign”).ChildNodes;
foreach (XmlElement item in nodeList)
{
if(item.GetAttribute(“name”)==AlterAccountNumber.text.Trim().ToString()&&item.GetAttribute(“passWord”)==AlterPassWord.text.Trim().ToString())
{
item.SetAttribute(“name”,NormalAccountNumber.text.Trim().ToString());
item.SetAttribute("passWord",NormalPassWord.text.Trim().ToString());
}
}
xmlDoc.Save(path);
}
}
///
/// 删除账号
///
public void DeleteAccountNumber()
{
//定义路径
string path = Application.dataPath + “/Xml/xml_player.xml”;
if(File.Exists(path))
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(path);
XmlNodeList nodeList = xmlDoc.SelectSingleNode(“Sign”).ChildNodes;
foreach (XmlElement item in nodeList)
{
if (item.GetAttribute(“name”) == AlterAccountNumber.text.Trim().ToString() && item.GetAttribute(“passWord”) == AlterPassWord.text.Trim().ToString())
{
item.RemoveAll();
}
}
xmlDoc.Save(path);
}
}
最后小小宣传下:一个致力于unity游戏,模型,等资源的淘宝店铺,店里有unity3d成品游戏(源工程,源代码),场景资源,模型资源等,一切资源都特别优惠,店铺链接:【unity3D素材包/unity3d中世纪村庄包Medieval Village Pack】https://m.tb.cn/h.eaENjDH?sm=2b946c 点击链接,再选择浏览器咑閞;或復·制这段描述¥kIKkbAV3c3C¥后到?淘♂寳♀?