XML:
优点:格式统一,符合标准,容易与其他系统进行远程交互,数据共享比较方便
缺点:XML文件庞大,文件格式复杂,传输占带宽;服务器端与客户端解析XML花费较多的资源和时间
Json:
优点:数据格式比较简单,易于读写,格式都是压缩的,占用带宽较小,支持多种语言
缺点:可读性较XML略差
总结:
两者可以相互转换,功能都是相差无几,但是json比xml较好,但是xml更加通用
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Xml;
using System.IO;
public class ReadXml:MonoBehaviour {
void Start () {
CreateXML();
//AddXML();
//ReadXML();
//UpdateXML();
//DeleteXML();
}
//生成XMl文件
void CreateXML()
{
XmlDocument doc = new XmlDocument();
XmlElement root = doc.CreateElement("team");
//对这个结构单元添加属性
root.SetAttribute("name", "Person");
XmlElement student = doc.CreateElement("student");
XmlElement name = doc.CreateElement("name");
name.InnerText = "xiaoming";
XmlElement age = doc.CreateElement("name");
age.InnerText = "16";
XmlElement id = doc.CreateElement("name");
id.InnerText = "11111111";
student.AppendChild(name);
student.AppendChild(age);
student.AppendChild(id);
root.AppendChild(student);
doc.AppendChild(root);
doc.Save(Application.dataPath + @"/testXML.xml");
Debug.Log("创建成功");
}
//读取XML
void ReadXML()
{
XmlDocument doc = new XmlDocument();
string xmlfile = Application.dataPath + @"/XML.xml";
doc.Load(xmlfile);
//获取根节点XmlElement表示节点(元素)
XmlNode team = doc.SelectSingleNode("team");
XmlNodeList personsEle = team.ChildNodes;
foreach (XmlElement personEle in personsEle)
{
Debug.Log(personEle.GetAttribute("student"));
XmlElement nameEle = personEle.ChildNodes[0] as XmlElement;
XmlElement ageEle = personEle.ChildNodes[1] as XmlElement;
XmlElement idEle = personEle.ChildNodes[2] as XmlElement;
Debug.Log(nameEle.InnerText + "," + ageEle.InnerText + "," + idEle.InnerText);
}
}
//添加XML
void AddXML()
{
XmlDocument doc = new XmlDocument();
doc.Load(Application.dataPath + @"/XML.xml");
XmlNode team = doc.SelectSingleNode("team");
XmlElement newStudent = doc.CreateElement("student");
XmlElement name = doc.CreateElement("name");
name.InnerText = "hexin";
XmlElement age = doc.CreateElement("age");
age.InnerText = "24";
XmlElement id = doc.CreateElement("id");
id.InnerText = "20151208";
//向新的student中添加属性
newStudent.AppendChild(name);
newStudent.AppendChild(age);
newStudent.AppendChild(id);
//将student添加到team中
team.AppendChild(newStudent);
doc.Save(Application.dataPath+@"/XMLAdd.xml");
}
//更新XML
void UpdateXML()
{
XmlDocument doc = new XmlDocument();
doc.Load(Application.dataPath + @"/XMLAdd.xml");
XmlNode team = doc.SelectSingleNode("team");
XmlNodeList personsEle = team.ChildNodes;
foreach (XmlElement personEle in personsEle)
{
XmlNode name = personEle.SelectSingleNode("name");
if (name.InnerText.Equals("hexin"))
{
Debug.Log("更改数据");
name.InnerText = "HEXIN";
}
}
doc.Save(Application.dataPath + @"/XMLUpdate.xml");
}
//删除XML节点
void DeleteXML()
{
XmlDocument doc = new XmlDocument();
doc.Load(Application.dataPath + @"/XML.xml");
XmlNode team = doc.SelectSingleNode("team");
XmlNodeList personsEle = team.ChildNodes;
foreach (XmlElement personEle in personsEle)
{
XmlNode name = personEle.SelectSingleNode("name");
if (name.InnerText.Equals("Lili"))
{
Debug.Log("删除数据");
team.RemoveChild(personEle);
}
}
doc.Save(Application.dataPath + @"/XMLDelete.xml");
}
}