unity,c#读写xml文件内容
讲一下我的学习路径。本文档适合,有编程基础,但是还不太会看官方文档的人,
有一个功能要用到这个,我先在原来的项目上面的看这部分功能的实现,看懂了,但是我不知道到它的这一套流程是哪里来的,还能用其他方法吗。所以我就去百度搜索一下。看了一下别人怎么实现,然后我就看到了大概第二种方法。我觉得应该要去找源头,及它的官方文档。根据前面两个实现的代码,找到对应的类,然后去看这些类的方法。然后大概懂了它的官方文档。
编写xml文档放到unity项目中
编写xml文件 文件名UIXML.xml
<OperStep width="4">
<BigStep name="用物准备">用物准备</BigStep>
<BigStep name="快速评估">评估指标,保暖,垫枕头</BigStep>
<BigStep name="初步复苏">评估试点,擦干,刺激,检查</BigStep>
<BigStep name="正压通气">指征,正压通气,评估心率,插气管导管,正压通气2,测心率</BigStep>
<BigStep name="胸外按压">胸外按压,注射,取出气管</BigStep>
</OperStep>
放在和assets同级目录下
unity读写xml文件
获取文件路径
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public class CreatUI : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
//获取路径
string path = GetPath();
ReadXmll readXmll = new ReadXmll();
readXmll.LoadXMLFile(path);
}
//获取文件路径, 后面可以封装的所有的获取路径
string GetPath(){
//@不进行转义\ ,获取assets同级目录下xml文件
string path = Directory.GetParent(Application.dataPath) +@"\UIXML.xml";
//测试路径是否获取正确
Debug.Log(path+" path");
return path;
}
}
读取其中的信息
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Xml;
using System.IO;
public class ReadXmll
{
public XmlDocument sceneCfgDocument;//读取xml文件的工具类,要导入using System.Xml;
public void LoadXMLFile(string strCfgUrl) {
sceneCfgDocument = new XmlDocument();
//加载字符串到XmlDocument对象里面,不留空白
sceneCfgDocument.LoadXml(File.ReadAllText(strCfgUrl));
//由节点名称获取节点里面的内容
var SenceNode = sceneCfgDocument.SelectSingleNode("OperStep");
var bigStep = sceneCfgDocument.SelectNodes("BigStep");
for (var i = 0; i < bigStep.Count; i++)
{
//获取节点里面的文本
string str = bigStep[i].InnerText;
if (str != "") ;
Debug.Log(str);
}
}
}
到这里学会初步获取xml里面的值
在c# 官方文档中有更多方法可以操作
https://docs.microsoft.com/zh-cn/dotnet/
在里面搜索 XmlDocument
该类可以 加载、验证、编辑、添加和放置 XML文档。先将文档元素加载到该类对象中在对它进行处理。
点开类说明,看一下命名空间Namespaces,看一下继承inherit
加载
下拉
看到将 XML 加载到文档对象模型中。 看到如下图中的文字点击
https://docs.microsoft.com/zh-cn/dotnet/standard/data/xml/reading-an-xml-document-into-the-dom
里面简要说明了
Load ,LoadXml ,XmlReader方法三种加载xml文档的方法
点击loadXml 就可以进入到详细说明,是XmlDocument的方法
XmlDocument doc = new XmlDocument();
doc.LoadXml("<item><name>wrench</name></item>");
即可读取
读取节点集合
XmlDocument 类 的方法
返回 XmlDocument 类 下拉到
https://docs.microsoft.com/zh-cn/dotnet/api/system.xml.xmldocument?view=net-6.0#Find
找到通过节点名称获取节点集合。这里的集合是是树的结构的。
doc.load(),是根据路径来加载到对象里面的。
获取节点集合
XmlNodeList elemList = doc.GetElementsByTagName("title");
获取节点里面的文本
elemList[i].InnerXml
可以从左侧类下面的方法中找详细说明。
还有许多如ReadNote,大家自己探索。
XmlDocument 类 继承的方法
由类XmlDocument开头可以看到,它继承自xmlNode,找到点击xmlNode,或者左侧找打
点击它的method,根据英文意思,我们可以猜测出这个两个方法可以查找出节点,这两个方法就是我们上面所用的方法。点击进入查看
var SenceNode = sceneCfgDocument.SelectSingleNode("OperStep");
var bigStep = sceneCfgDocument.SelectNodes("BigStep");
它的参数都是用xpath表达式,看不懂下拉请看
![在这里插入图片描述](https://img-blog.csdnimg.cn/caa0e610efbd462d8b26cdd39c67b82d.png#pic_center
https://docs.microsoft.com/zh-cn/previous-versions/dotnet/netframework-4.0/ms256086(v=vs.100)
我们使用这个。
当前上下文是什么意思。
读取节点的属性值
单节点.Attributes[“属性名字”].Value
var bigStep = sceneCfgDocument.SelectNodes("BigStep");
for (var i = 0; i < bigStep.Count; i++)
{
//获取节点里面的文本
string str = bigStep[i].Attributes["title"].Value;
if (str != "") ;
Debug.Log(str);
}