创建读取修改添加Xml表
直接上代码,话不多说,注释写的应该差不多
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Xml;
using System.IO;//需要引入
public class ReadXmlManager : MonoBehaviour
{
public List<People> people = new List<People>();
void Start()
{
CreateXml();
ReadXml();
//ChangeXml();
AddPeopleXml();
}
void CreateXml()
{
string path = Application.dataPath + "/Resources/people.xml";
if (!File.Exists(path))
{
XmlDocument xmlDocument = new XmlDocument();
XmlElement parent = xmlDocument.CreateElement("Root");//创建一个根节点
XmlElement node = xmlDocument.CreateElement("people");//创建people节点
node.SetAttribute("id", "1");//把id作为people的一个属性
//这里不把id单独创建一个节点,是为了根据id修改数据简单一点
//XmlElement node0 = xmlDocument.CreateElement("id");
//node0.InnerText = "1";
XmlElement node1 = xmlDocument.CreateElement("name");//创建3个节点,name age score
node1.InnerText = "aaaaa";//设置当前节点内容
XmlElement node2 = xmlDocument.CreateElement("age");
node2.InnerText = "20";
XmlElement node3 = xmlDocument.CreateElement("score");
node3.InnerText = "90";
node.AppendChild(node1);//将3个节点添加到people节点下,节点顺序跟添加顺序一致
node.AppendChild(node2);
node.AppendChild(node3);
parent.AppendChild(node);//将people节点添加到根节点下
xmlDocument.AppendChild(parent);
xmlDocument.Save(path);//保存
}
}
void ReadXml()
{
string path = Application.dataPath + "/Resources/people.xml";
if (File.Exists(path))//判断文件路径是否存在
{
XmlDocument xml = new XmlDocument();
xml.Load(path);//按照xml路径读取文件
XmlNodeList root = xml.FirstChild.ChildNodes;//获取第一个节点的所有子节点,也就是根节点root的所有子节点
people.Clear();//清空存储列表
foreach (XmlElement it in root)//遍历所有子节点
{
if (it.Name == "people")//如果节点名字是people,则再单独遍历people的子节点
{
People peo = new People();
peo.id = int.Parse(it.GetAttribute("id"));
foreach (XmlElement element in it)
{
if (element.Name == "name")//找到对应名称的节点,将节点内容写到peo里
{
peo.name = element.InnerText;
}
else if (element.Name == "age")
{
peo.age = int.Parse(element.InnerText);
}
else if (element.Name == "score")
{
peo.score = int.Parse(element.InnerText);
}
}
people.Add(peo);//保存
}
}
}
}
void ChangeXml()
{
string path = Application.dataPath + "/Resources/people.xml";
if (File.Exists(path))//判断文件路径是否存在
{
XmlDocument xml = new XmlDocument();
xml.Load(path);//按照xml路径读取文件
XmlNodeList root = xml.FirstChild.ChildNodes;//获取第一个节点的所有子节点,也就是根节点root的所有子节点
foreach (XmlElement it in root)//遍历所有子节点
{
//这是把id单独作为一个节点的修改方法
//if (it.Name == "people")//如果节点名字是people,则再单独遍历people的子节点
//{
// List<XmlElement> eles = new List<XmlElement>();
// foreach (XmlElement element in it)
// {
// eles.Add(element);//暂存所有子节点
// }
// if(eles.Count == 4 && eles[0].InnerText == "1")//按照顺序,eles[0]是id节点,如果节点数目正确,并且id为1,则修改名字
// {
// eles[1].InnerText = "改了名字";
// }
//}
//当id是属性时候的修改方法
if (it.Name == "people" && it.GetAttribute("id") == "1")
{
foreach (XmlElement ele in it)
{
if (ele.Name == "name")
{
ele.InnerText = "改了名字+1";
}
}
}
}
xml.Save(path);//记得修改完要保存,否则修改不会生效
}
}
void AddPeopleXml()
{
string path = Application.dataPath + "/Resources/people.xml";
if (File.Exists(path))
{
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(path);
//可以直接找到要添加的节点,克隆一个,修改数值,再添加进去
XmlNode ele = xmlDocument.FirstChild.FirstChild.Clone();
(ele as XmlElement).SetAttribute("id", "2");
foreach(XmlElement it in ele)
{
if (it.Name == "name")
{
it.InnerText = "bbbbb";
}
else if (it.Name == "age")
{
it.InnerText = "21";
}
else if (it.Name == "score")
{
it.InnerText = "100";
}
}
xmlDocument.FirstChild.AppendChild(ele);
xmlDocument.Save(path);//保存
//也可以重新创建要添加的节点,保持格式一致
//XmlElement node = xmlDocument.CreateElement("people");//创建people节点
//node.SetAttribute("id", "2");//把id作为people的一个属性
//XmlElement node1 = xmlDocument.CreateElement("name");//创建3个节点,name age score
//node1.InnerText = "bbbbb";//设置当前节点内容
//XmlElement node2 = xmlDocument.CreateElement("age");
//node2.InnerText = "21";
//XmlElement node3 = xmlDocument.CreateElement("score");
//node3.InnerText = "100";
//node.AppendChild(node1);//将3个节点添加到people节点下,节点顺序跟添加顺序一致
//node.AppendChild(node2);
//node.AppendChild(node3);
//xmlDocument.FirstChild.AppendChild(node);//将people节点添加到根节点下
//xmlDocument.Save(path);//保存
}
}
}
创建成功
修改成功
添加成功