Xml与Json的使用

一.使用Json的时候需要引入一个动态链接库LitJson百度一下都能找到,放在程序目录即可使用

1.首先我们创建一个XMl文件

public void createXML()
{
//xml保存的路径,放在asset的路径下面
string filepath = Application.dataPath + @"/test.xml";
//判断当前路径下是否有该文件
if(!File.Exists(filepath))
{
//创建xml文档实例
XmlDocument xmlDoc = new XmlDocument();
//创建root节点,也就是最上一层节点
XmlElement root = xmlDoc.CreateElement("transforms");
//继续创建下一层节点
XmlElement elmItem = xmlDoc.CreateElement("rotation");
//设置节点的两个属性ID与Name
elmItem.SetAttribute("id", "0");
elmItem.SetAttribute("name", "colo");


//继续创建下一层节点

XmlElement rotation_X = xmlDoc.CreateElement("x");
//设置节点数值
rotation_X.InnerText = "0";

XmlElement rotation_Y = xmlDoc.CreateElement("y");
//设置节点数值
rotation_Y.InnerText = "1";

XmlElement rotation_Z = xmlDoc.CreateElement("z");
//设置节点数值
rotation_Z.InnerText = "2";
rotation_Z.SetAttribute("id", "1");

//把创建的节点添加到xmldoc中
elmItem.AppendChild(rotation_X);
elmItem.AppendChild(rotation_Y);
elmItem.AppendChild(rotation_Z);
root.AppendChild(elmItem);
xmlDoc.AppendChild(root);
//把xml保存到本地
xmlDoc.Save(filepath);
Debug.Log("Create Xml Ok !");

}
}

2.接着我们就有可以根据我们创建的,来进行更新xml的内容

public void UpdateXml()
{
string filepath = Application.dataPath + @"/test.xml";
if(File.Exists(filepath))
{
//创建xml文档实例
XmlDocument xmlDoc = new XmlDocument();
//根据路径将xml读取出来
xmlDoc.Load(filepath);
//得到transform下面的所有子节点
XmlNodeList nodelist = xmlDoc.SelectSingleNode("transforms").ChildNodes;
//遍历所有子节点
foreach (XmlElement item in nodelist)
{
//取得子节点中id = 0的节点
if(item.GetAttribute("id") == "0")
{
//更新子节点
item.SetAttribute("id", "520");
//继续遍历id = 0的节点的子节点
foreach (XmlElement xe in item.ChildNodes)
{
if(xe.Name == "z")
{
xe.SetAttribute("id", "521");
xe.InnerText = "coloooo";
}
}
break;
}
}
xmlDoc.Save(filepath);
Debug.Log("Update Xml Ok!");
}
}

3.我们也可以对xml再次添加,跟一开始创建的程序差不多

public void AddXml()
{
string filepath = Application.dataPath + @"/test.xml";

if(File.Exists(filepath))
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filepath);

XmlNode root = xmlDoc.SelectSingleNode("transforms");
XmlElement elemNew = xmlDoc.CreateElement("rotation");
elemNew.SetAttribute("id", "1");
elemNew.SetAttribute("name", "xolooo");

XmlElement rotation_x = xmlDoc.CreateElement("x");
rotation_x.InnerText = "0";
rotation_x.SetAttribute("id", "0000");
XmlElement rotation_y = xmlDoc.CreateElement("y");
rotation_y.InnerText = "1";
XmlElement rotation_z = xmlDoc.CreateElement("z");
rotation_z.InnerText = "2";

elemNew.AppendChild(rotation_x);
elemNew.AppendChild(rotation_y);
elemNew.AppendChild(rotation_z);
root.AppendChild(elemNew);
xmlDoc.AppendChild(root);
xmlDoc.Save(filepath);
Debug.Log("AddXml Ok !");
}

}

4.对我们之前的xml进行删除处理

public void DeleteXml()
{
string filepath = Application.dataPath + @"/test.xml";
if(File.Exists(filepath))
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filepath);

XmlNodeList nodeList = xmlDoc.SelectSingleNode("transforms").ChildNodes;
foreach (XmlElement item in nodeList)
{
if(item.GetAttribute("id") == "1")
{
item.RemoveAttribute("id");
}
foreach (XmlElement xe in item)
{
if(xe.Name == "z")
{
xe.RemoveAll();
}
}
}
xmlDoc.Save(filepath);
Debug.Log("deleteXMl Ok!");
}
}

5.对xml进行整体输出

public void ShowXml()
{
string filepath = Application.dataPath + @"/test.xml";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filepath);

XmlNodeList nodeList = xmlDoc.SelectSingleNode("transforms").ChildNodes;
foreach (XmlElement item in nodeList)
{
Debug.Log("Name :" + item.Name + " " + "Attribute :" + item.GetAttribute("name"));
//Debug.Log("Name :" + item.Name);
foreach (XmlElement xe in item.ChildNodes)
{
if(xe.Name == "y")
{
Debug.Log("y of " + item.GetAttribute("name") + " :" + xe.InnerText);
}
}
}
Debug.Log("all =" + xmlDoc.OuterXml);
}

6.Json字符串的自定义(字符串定义,还有程序定义,步骤7中介绍),以及对json的输出,此时需要强调的是Json的格式

public void ResolveJson()
{
//定义Json字符串
string str = @"
{
""Name"" : ""coqoooo"",
""Age"" : 20,
""Birthday"" : ""1995-1-1"",
""Thu"" : [
{
""Url"": ""http1"",
""Height"": 251
},
{
""Url"": ""http2"",
""Height"": 252
}

]
}";

//解析
JsonData jd = JsonMapper.ToObject(str);
Debug.Log("Name = " + jd["Name"]);
Debug.Log("Age = " + jd["Age"]);
Debug.Log("Birthday = " + jd["Birthday"]);
JsonData jdItems = jd["Thu"];

for (int i = 0; i < jdItems.Count; i ++)
{
Debug.Log("Url = " + jdItems[i]["Url"]);
Debug.Log("Height = " + jdItems[i]["Height"]);
}

}

7.Json创建的第二种方式,这种方式适用于少量item的,如果多了建议还是步骤6的方式

public void MergerJson()
{
//合成Json字符串,先合成然后在输出
StringBuilder sb = new StringBuilder();
JsonWriter writer = new JsonWriter(sb);

writer.WriteObjectStart();

writer.WritePropertyName("Name");
writer.Write("CQ");

writer.WritePropertyName("Age");
writer.Write(26);

writer.WritePropertyName("Girl");

writer.WriteArrayStart();

writer.WriteObjectStart();
writer.WritePropertyName("name");
writer.Write("rourou");
writer.WritePropertyName("age");
writer.Write(22);
writer.WriteObjectEnd();

writer.WriteObjectStart();
writer.WritePropertyName("name");
writer.Write("loulou");
writer.WritePropertyName("age");
writer.Write(23);
writer.WriteObjectEnd();

writer.WriteArrayEnd();

writer.WriteObjectEnd();
Debug.Log(sb.ToString());

}

原文地址:https://www.cnblogs.com/xwwFrank/p/4543397.html