一般我们在游戏或者软件制作中,一般使用JSON和XML会比较多,Json的话比较轻量,Xml的话结构版式会复杂点,下面是xml读和写!!!
using UnityEngine;
using System.Collections;
using System;
using System.IO;
using System.Xml;
using System.Collections.Generic;
public class XmlCreate:MonoBehaviour
{
public void CreateXml()
{
//第一种写法 直接插入
//注意xml都是字符类型的
string uarl = Application.streamingAssetsPath + "/ly.xml";
XmlDocument pDocument = new XmlDocument();
XmlDeclaration pDeclaration = pDocument.CreateXmlDeclaration("1.0", "UTF-8", null);//定制我们的版式说明是必须的
pDocument.AppendChild(pDeclaration);//版式说明建立
//根节点建立
XmlElement pRoot = pDocument.CreateElement("Books");
pDocument.AppendChild(pRoot);//将根节点插进去
XmlElement pBook = pDocument.CreateElement("book");
pRoot.AppendChild(pBook);//书的根节点
//XmlAttribute pBookAttribute = pDocument.CreateAttribute("id");//创建一个属性
//pBookAttribute.Value = System.Convert.ToString(01);
//pBook.Attributes.Append(pBookAttribute);//将属性插到指定节点中
//XmlElement pBookName = pDocument.CreateElement("name");
//pBookName.InnerText = "金瓶梅";
//pRoot.AppendChild(pBook);
//XmlElement pBookPice = pDocument.CreateElement("pice");
//pBookPice.InnerText = System.Convert.ToString(0.5f);
//pBook.AppendChild(pBookPice);
//XmlElement pBookMemo = pDocument.CreateElement("Memo");
//pBookMemo.InnerText = "听说这是一本插画很美的书 适用于18+ 购买时请注意 *_*";
//pBook.AppendChild(pBookMemo);
//第二种写法 用字典和元素构造
Dictionary<string, string> pContext = new Dictionary<string, string>();
pContext.Add("name", "金瓶梅");
pContext.Add("pice", System.Convert.ToString(0.5f));
pContext.Add("Memo", "听说这是一本插画很美的书 适用于18+ 购买时请注意 *_*");
AppendChildNode(pDocument, pBook, ref pContext);
pBook = pDocument.CreateElement("book");
pRoot.AppendChild(pBook);
pContext.Clear();
pContext.Add("name", "优衣库");
pContext.Add("pice", System.Convert.ToString(0.8f));
pContext.Add("Memo", "听说这是一间秀色可餐的衣服间 参观者18+ 心脏不好时请注意 *_*");
AppendChildNode(pDocument, pBook, ref pContext);
pBook = pDocument.CreateElement("book");
pRoot.AppendChild(pBook);
pContext.Clear();
pContext.Add("name", "西游记");
pContext.Add("pice", System.Convert.ToString(0.3f));
pContext.Add("Memo", "听说这是一本根据历史事实改编的古典小说 观看者18+ 模仿者时请注意 *_*");
AppendChildNode(pDocument, pBook, ref pContext);
pDocument.Save(uarl);
}
//Xml元素构造 -写
public bool AppendChildNode(XmlDocument pDcument,XmlNode nParent,ref Dictionary<string,string>pKeyValue)
{
bool isRet = false;
do
{
if (null == pDcument || nParent == null || null ==pKeyValue) break;
if (pKeyValue.Count <= 0) break;
string[] keys = new string[pKeyValue.Count];
pKeyValue.Keys.CopyTo(keys, 0);
for (int i = 0; i < keys.Length; ++i)
{
XmlElement pNode = pDcument.CreateElement(keys[i]);
pNode.InnerText = pKeyValue[keys[i]];
nParent.AppendChild(pNode);
}
isRet = true;
}
while (false);
return isRet;
}
//读取XML -读
public void ReadXml()
{
string strPath = Application.streamingAssetsPath+ "/ly2.xml";
XmlDocument pDocument = new XmlDocument();
pDocument.Load(strPath);
XmlElement pRoot = pDocument.DocumentElement;//获取我们第一个根节点
Debug.Log(pRoot.Name);
XmlNodeList pChildList = pRoot.ChildNodes;//获取根节点下面的所有子节点
for (int i = 0; i < pChildList.Count; ++i)
{
XmlElement pBookNode = pChildList[i] as XmlElement;
XmlNodeList pNode = pBookNode.ChildNodes;
if (null != pNode && pNode.Count > 0)
{
for (int j = 0; j < pNode.Count; ++j)
{
XmlElement ppNode = pNode[j] as XmlElement;
Debug.Log(ppNode.Name);
Debug.Log("InnerText==> "+ppNode.InnerText);
//属性==>
if (ppNode.HasAttributes)//获取我们的属性
{
XmlAttribute[] pAttibute = new XmlAttribute[ppNode.Attributes.Count];
ppNode.Attributes.CopyTo(pAttibute, 0);
for (int n = 0; n < pAttibute.Length; ++n)
{
Debug.Log("Attitube"+pAttibute[n].Value);
}
}
}
}
}
}
网友评论