Unity 代码操作XML文件
【摘要】 第一个方法是,在什么都没有的情况下创建一个XML文件,, 第二个方法是,在上面创建好的根目录下,新建一段内容,,当然你还可以修改原来的属性或者删除,都是由对应的方法的,, 详情:修改删除方法
实现代码:
using UnityEngine;
using System.Xml; //注意引用命名空间
public class createXml : Mo...
第一个方法是,在什么都没有的情况下创建一个XML文件,,
第二个方法是,在上面创建好的根目录下,新建一段内容,,当然你还可以修改原来的属性或者删除,都是由对应的方法的,,
详情:修改删除方法
实现代码:
using UnityEngine;
using System.Xml; //注意引用命名空间
public class createXml : MonoBehaviour { public string path; //路径名 // Use this for initialization void Start () { path = Application.dataPath + "/czhenya.xml"; } private void OnGUI() { //new Rect(按钮的位置,按钮的大小) if (GUI.Button(new Rect(0, 0, 100, 40),"CreateXML")) { //点击OnGUI生成按钮,创建自己的XML文件 Create(); } if (GUI.Button(new Rect(0, 50, 100, 40), "CreateXML")) { //点击OnGUI生成按钮,创建自己的XML文件 Addxml(); } } /// <summary> /// 生成XML文件的方法 /// </summary> private void Create() { XmlDocument doc = new XmlDocument(); XmlElement root = doc.CreateElement("root"); //创建根节点 XmlElement book = doc.CreateElement("book"); //创建子节点 XmlElement title = doc.CreateElement("title"); XmlElement author = doc.CreateElement("autor"); XmlElement year = doc.CreateElement("year"); XmlElement price = doc.CreateElement("price"); //设置节点之间的关系 book.AppendChild(title); //为book设置自己的子节点 book.AppendChild(author); book.AppendChild(year); book.AppendChild(price); root.AppendChild(book); //为book,设置符节点(根节点) doc.AppendChild(root); //把根节点(root)添加到XML文件中 //为创建来的各个节点添加文本 book.SetAttribute("categroy", "悬疑"); title.InnerText = "盗墓笔记"; author.InnerText = "南派三叔"; year.InnerText = "2007"; price.InnerText = "199.99"; //一定记得保存 doc.Save(path); } /// <summary> /// 添加文本 /// </summary> private void Addxml() { XmlDocument doc = new XmlDocument(); doc.Load(path); //加载 ,添加在原来的根目录下,, XmlElement root = doc.SelectSingleNode("root") as XmlElement; //选择根节点 XmlElement book = doc.CreateElement("book"); //新建节点 XmlElement title = doc.CreateElement("title"); XmlElement author = doc.CreateElement("autor"); XmlElement year = doc.CreateElement("year"); XmlElement price = doc.CreateElement("price"); //设置节点之间的关系 book.AppendChild(title); //为book设置自己的子节点 book.AppendChild(author); book.AppendChild(year); book.AppendChild(price); root.AppendChild(book); //为book,设置符节点(根节点) doc.AppendChild(root); //把根节点(root)添加到XML文件中 //为创建来的各个节点添加文本 book.SetAttribute("categroy", "历史"); title.InnerText = "三国演义"; author.InnerText = "罗贯中"; year.InnerText = "1366"; price.InnerText = "999.99"; doc.Save(path); }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
效果图:
文章来源: czhenya.blog.csdn.net,作者:陈言必行,版权归原作者所有,如需转载,请联系作者。
原文链接:czhenya.blog.csdn.net/article/details/78040013
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)