路径选择
- 存储文件夹选择
Resources 只读 不可写入
Application.streamingAssetsPath 可读 pc端可写 在ios、安卓不可写
Application.dataPath 打包后找不到
Application.persistentDataPath 可读可写 适合多个平台
Application.persistentDataPath+"/文件名.XML"
存储
- 创建文本对象
XmlDocument xml=new XmlDocument(); - 添加xml版本信息
XmlDeclaration dec=xml.CreateXmlDeclaration(“版本”,“编码方式”,“”);//参数三空字符串 - 版本信息添加到文本中
xml.AppendChilde(xmlDec); - 添加根节点
XmlElement root=xml.CreateElement(“节点”);//节点显示在xml文档中<节点></节点>
xml.AppendChild(root); - 根节点添加子节点
XmlElement age=xml.CreateElement(“Age”);
age.InnerText=“12”;//添加元素<age>12</age>
root.AppendChild(age); - 子节点添加子节点
XmlElement name=xml.CreateElement(“Name”);
name.SetAttribute(“属性名”,字符串);//添加属性<name "属性名"=字符串></name>
age.AppendChild(name); - 保存xml文本
xml.Save(路径);
修改
- 先判断文件是否存在
File.Exists(路径); - 加载xml文件
XmlDocument loadXml=new XmlDocument();
loadXml.Load(路径); - 移除节点
XmlNode root=loadXml.SelectSingleNode(“根节点”);
XmlNode node=loadXml.SelectSingleNode(“根节点”).SelectSingleNode(“节点”);//获取节点 方法1
node=loadXml.SelectSingleNode(“根节点/节点”);//获取节点 方法2
root.RemoveChild(node);//移除子节点 父节点移除子节点 - 添加节点
XmlElement sex=loadXml.CreateElement(“Sex”);
sex.InnerText=“Male”;
root.AppendChild(sex); - 修改后需要保存
loadXml.Save(路径);