1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
| using System.IO; using System.Xml; using UnityEngine; using System.Collections;
public class XmlFile : MonoBehaviour {
private void Start() { //print(Application.dataPath); CreateXml(); //UpdateXml(); //RemoveXml(); //AddXml(); }
//创建xml private void CreateXml() { /** * @避免字符串转义 * @允许字符串换行 * @可读性更高 */ string filePath = Application.dataPath + @"/File/creat.xml"; if (!File.Exists(filePath)) { XmlDocument xmlDoc = new XmlDocument();
//创建节点 XmlElement root = xmlDoc.CreateElement("root");
for (int i = 0; i < 5; i++) { XmlElement student = xmlDoc.CreateElement("student"); //设置属性 student.SetAttribute("id", i+1+"");
XmlElement name = xmlDoc.CreateElement("name"); //设置内容 name.InnerText = "小明"; XmlElement sex = xmlDoc.CreateElement("sex"); sex.InnerText = "男";
//添加节点 student.AppendChild(name); student.AppendChild(sex); root.AppendChild(student); } xmlDoc.AppendChild(root);
//xml保存到本地 xmlDoc.Save(filePath); } }
//更新xml private void UpdateXml() { string filePath = Application.dataPath + @"/File/creat.xml"; if (File.Exists(filePath)) { XmlDocument xmlDoc = new XmlDocument();
//读取文件 xmlDoc.Load(filePath);
//得到所有节点 XmlNodeList nodeList = xmlDoc.SelectSingleNode("root").ChildNodes;
//遍历节点 foreach (XmlElement ele in nodeList) {
if (ele.GetAttribute("id") == "3") { foreach (XmlElement elea in ele.ChildNodes) { if (elea.Name == "sex") { elea.InnerText = "女"; } } //跳出循环 break; } Debug.Log("是否跳出循环"); }
//保存文件 xmlDoc.Save(filePath); } }
//删除节点 private void RemoveXml() { string filePath = Application.dataPath + @"/File/creat.xml"; if (File.Exists(filePath)) { XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filePath);
XmlNodeList nodeList = xmlDoc.SelectSingleNode("root").ChildNodes; foreach (XmlElement ele in nodeList) { if (ele.GetAttribute("id") == "4") { foreach (XmlElement elea in ele.ChildNodes) { if (elea.Name == "name") { elea.RemoveAttribute("age"); } if (elea.Name == "sex") { elea.RemoveAll(); } } //ele.RemoveChild(ele.LastChild); ele.RemoveChild(ele.SelectSingleNode("sex")); break; } } xmlDoc.Save(filePath); } }
//添加文件 private void AddXml() { string filePath = Application.dataPath + @"/File/creat.xml"; if (File.Exists(filePath)) { XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filePath);
XmlNode root = xmlDoc.SelectSingleNode("root"); int len = xmlDoc.SelectSingleNode("root").ChildNodes.Count; for (int i = len; i < len+2; i++) { XmlElement student = xmlDoc.CreateElement("student"); //设置属性 student.SetAttribute("id", i + 1 + "");
XmlElement name = xmlDoc.CreateElement("name"); //设置内容 name.InnerText = "小花"; XmlElement sex = xmlDoc.CreateElement("sex"); sex.InnerText = "男";
//添加节点 student.AppendChild(name); student.AppendChild(sex); root.AppendChild(student); } xmlDoc.Save(filePath); } } }
|