using System.Collections; using System.Collections.Generic; using UnityEngine; using System.Xml;
public class xmll : MonoBehaviour { // Start is called before the first frame update void Start() { LoadXml("/coom.xml"); ModifyXML("/coom.xml"); DeleteXML("/coom.xml"); AddXML("/coom.xml"); EstablishXML(); }
void LoadXml(string path)
{
XmlDocument xd = new XmlDocument();
//加载路径
xd.Load(Application.streamingAssetsPath + path);
//查找根节点并得到
XmlNode RootNode = xd.SelectSingleNode("root");
//查找初始状态子集点
XmlNodeList BeginNode = RootNode.SelectNodes("BeginStatus");
//遍历子节点并添加到队列中,获取详细信息
foreach (XmlNode item in BeginNode)
{
XmlElement BeginXmlEle = (XmlElement)item;
BeginStatusobj bs = new BeginStatusobj()
{
id = int.Parse(BeginXmlEle.GetAttribute("id")),
ObjName = BeginXmlEle.GetAttribute("Name"),
Type = BeginXmlEle.GetAttribute("Type"),
};
print(bs.id);
print(bs.ObjName);
print(bs.Type);
}
XmlNodeList StepNode1 = RootNode.SelectNodes("Step");
//遍历子节点并添加到队列中,获取详细信息
foreach (XmlNode item in StepNode1)
{
XmlElement StepXmlEle = (XmlElement)item;
Pract bs = new Pract()
{
id = int.Parse(StepXmlEle.GetAttribute("id")),
ObjName = StepXmlEle.GetAttribute("Name"),
Type = StepXmlEle.GetAttribute("Type"),
};
//若此条xml数据中有entext 则执行这一步
if (StepXmlEle.HasAttribute("entext"))
{
bs.entext = StepXmlEle.GetAttribute("entext");
}
if (StepXmlEle.HasAttribute("Angle"))
{
bs.Angle =int.Parse(StepXmlEle.GetAttribute("Angle"));
}
print(bs.id);
print(bs.ObjName);
print(bs.Type);
print(bs.entext);
print(bs.Angle);
}
XmlNodeList EndNode = RootNode.SelectNodes("End");
foreach (XmlNode item in EndNode)
{
XmlElement EndElem = (XmlElement)item;
//拆解整条数据
print(EndElem.GetAttribute("id"));
print(EndElem.GetAttribute("Name"));
print(EndElem.GetAttribute("Type"));
}
}
//修改XML
void ModifyXML(string path)
{
//方法一 全局查找 不管有多少个子节点
//XmlDocument xd = new XmlDocument();
加载路径
//xd.Load(Application.streamingAssetsPath + path);
查找根节点并得到
//XmlNodeList xl = xd.SelectSingleNode("root").ChildNodes;
//foreach (XmlElement item in xl)
//{
// if (item.GetAttribute("id")=="1")
// {
// item.SetAttribute("id", "5");
// }
//}
//方法二 定向查找 适合多个子节点
XmlDocument xd = new XmlDocument();
//加载路径
xd.Load(Application.streamingAssetsPath + path);
//查找根节点并得到
XmlNode RootNode = xd.SelectSingleNode("root");
//查找初始状态子集点
XmlNodeList BeginNode = RootNode.SelectNodes("BeginStatus");
//遍历子节点并添加到队列中,获取详细信息
foreach (XmlNode item in BeginNode)
{
XmlElement BeginXmlEle = (XmlElement)item;
if (BeginXmlEle.GetAttribute("id")=="1")
{
BeginXmlEle.SetAttribute("id", "5");
}
}
xd.Save(Application.streamingAssetsPath + path);
}
//删除节点
void DeleteXML(string path)
{
XmlDocument xd = new XmlDocument();
//加载路径
xd.Load(Application.streamingAssetsPath + path);
//查找根节点并得到
XmlNode RootNode = xd.SelectSingleNode("root");
//查找初始状态子集点
XmlNodeList BeginNode = RootNode.SelectNodes("BeginStatus");
//遍历子节点并添加到队列中,获取详细信息
foreach (XmlNode item in BeginNode)
{
XmlElement BeginXmlEle = (XmlElement)item;
if (BeginXmlEle.GetAttribute("id") == "5")
{
BeginXmlEle.RemoveAll();//只会删除字段 不会删除节点
BeginXmlEle.ParentNode.RemoveChild(BeginXmlEle);//会将子节点删除
}
}
xd.Save(Application.streamingAssetsPath + path);
}
//增加子节点
void AddXML(string path)
{
XmlDocument xd = new XmlDocument();
//加载路径
xd.Load(Application.streamingAssetsPath + path);
//查找根节点并得到
XmlNode RootNode = xd.SelectSingleNode("root");
//创建根节点下的子节点
XmlElement xe = xd.CreateElement("Star");
xe.SetAttribute("id", "2");
xe.SetAttribute("Name", "po");
xe.SetAttribute("Type", "54");
RootNode.AppendChild(xe);//添加元素
//在已有的子节点添加数据
XmlElement xet = xd.CreateElement("End");
RootNode.AppendChild(xet);
for (int i = 0; i < RootNode.ChildNodes.Count; i++)
{
XmlElement xmlelem = RootNode.ChildNodes[i] as XmlElement;
if (xmlelem.GetAttribute("id")=="1")//在所有id=1的下面添加新的数据
{
XmlElement xmlt = xd.CreateElement("End");
xmlt.SetAttribute("id", "2");
xmlelem.AppendChild(xmlt);
}
}
xd.Save(Application.streamingAssetsPath + path);
}
//创建xml文件
void EstablishXML()
{
XmlDocument xd = new XmlDocument();
//创建最上面一层的节点
XmlElement root = xd.CreateElement("Root");
//创建子节点
XmlElement element = xd.CreateElement("Start");
//设置节点属性
element.SetAttribute("id", "1");
element.SetAttribute("Name", "op");
element.SetAttribute("Type", "ui");
//设置数据内面
element.InnerText = "kldjfkldsj";
XmlElement element1 = xd.CreateElement("Start");
//设置节点属性
element1.SetAttribute("id", "2");
element1.SetAttribute("Name", "op");
element1.SetAttribute("Type", "ui");
root.AppendChild(element1);
root.AppendChild(element);
xd.AppendChild(root);
xd.Save(Application.streamingAssetsPath + "/coo.xml");
}
}
public class BeginStatusobj { public int id; public string ObjName; public string Type; }
public class Pract { public int id;
public string ObjName;
public string Type;
public string entext;
public int Angle;
}
xml 文件 coom
coo 创建后的
|