XML读取:
string Path = Application.dataPath + "/Resources/Bag.xml";
XmlDocument doc = new XmlDocument();
doc.Load(Path);
XmlNode root = doc.SelectSingleNode("Items");
XmlNodeList nodelist = root.ChildNodes;
foreach(XmlNode node in nodelist)
{
XmlNodeList nnode = node.ChildNodes;
item_information item = new item_information();
item.ID = int.Parse(nnode[0].InnerText);
item.Name = nnode[1].InnerText;
item.Icon = nnode[2].InnerText;
item.Count = int.Parse(nnode[3].InnerText);
BagID.Add(item.ID);
bagDic.Add(item.ID, item);
}
XML添加节点:
void AddNote()
{
string path = Application.dataPath + "/MyPerson.xml";
//系统提供类来进行解析xml文件
XmlDocument dot = new XmlDocument();
//加载路径
dot.Load(path);
//获取根节点
XmlNode root = dot.SelectSingleNode("Items");
//创建一个新的的节点
XmlElement newNode = dot.CreateElement("Item");
//把Item节点添加到Items下面
root.AppendChild(newNode);
//在Item下面添加四个节点
XmlElement ID = dot.CreateElement("ID");
ID.InnerText = "1007";
XmlElement Name = dot.CreateElement("Name");
Name.InnerText = "屠龙宝刀";
XmlElement Icon = dot.CreateElement("Icon");
Icon.InnerText = "skill-01";
XmlElement Description = dot.CreateElement("Description");
Description.InnerText = "好一把屠龙宝刀";
//把这四个节点添加到Item下面
newNode.AppendChild(ID);
newNode.AppendChild(Name);
newNode.AppendChild(Icon);
newNode.AppendChild(Description);
dot.Save(path);
}
XML删除节点:
void DeletNode()
{
string path = Application.dataPath + "/MyPerson.xml";
XmlDocument dot = new XmlDocument();
dot.Load(path);
XmlNode root = dot.SelectSingleNode("Items");
XmlNodeList list = root.ChildNodes;
root.RemoveChild(list[6]);
dot.Save(path);
}
XML修改一个节点:
void XiuGaiNode()
{
string path = Application.dataPath + "/MyPerson.xml";
XmlDocument dot = new XmlDocument();
dot.Load(path);
XmlNode root = dot.SelectSingleNode("Items");
XmlNodeList ItemList = root.ChildNodes;
foreach (XmlNode Item in ItemList)
{
XmlNodeList list = Item.ChildNodes;
if (list[0].InnerText == "1001")
{
list[0].InnerText = "1000";
}
}
dot.Save(path);
}
