首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >uinty中对Xml文件的操作

uinty中对Xml文件的操作

作者头像
bering
发布2019-12-03 15:16:45
1K0
发布2019-12-03 15:16:45
举报
文章被收录于专栏:游戏开发之旅游戏开发之旅

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.csdn.net/CJB_King/article/details/52091158

最近学习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()等方法;

本文出自 “51CTO_King” 博客,请务必保留此出处http://cjboking.blog.51cto.com/11020113/1784368

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-08-02 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档