c# - 我的 xml 文件的小而简单的序列化程序有问题
问题描述
我正在使用一个简单的序列化程序将 xml 文件加载到我的游戏中(我正在实现 mod 支持)。我第一次拥有它的方式(它不是我自己的序列化程序)是这样的:
public static EnemyList Load(string path)
{
TextAsset _xml = Resources.Load<TextAsset>(path);
XmlSerializer serializer = new XmlSerializer(typeof(EnemyList));
StringReader reader = new StringReader(_xml.text);
EnemyList enemies = serializer.Deserialize(reader) as EnemyList;
reader.Close();
return enemies;
}
问题在于正在使用“Resources.Load”。由于我希望玩家编写/安装模组,因此不能在此处使用 Resources 文件夹(因为据我所知,他们无法访问 Resources 文件夹)。因此,我在 build 文件夹中创建了一个“Mods”文件夹,并在该 Mods 文件夹中创建了其他人的文件夹(例如,如果我要制作一个 mod,我将有一个像“MyMod”这样的文件夹,并且该文件夹将有像“entities”这样的其他文件夹一个“entities.xml”文件)被定位。为了从一个文件夹中获取所有文件,我使用了 DirectoryInfo。这是我的问题:我正在尝试更改序列化程序以使用 DirectoryInfo。这是到目前为止的代码:
public static EnemyList LoadXml(string path)
{
DirectoryInfo direcInfo = new DirectoryInfo(path);
FileInfo[] fileInfo = direcInfo.GetFiles();
XmlSerializer serializer = new XmlSerializer(typeof(EnemyList));
StringReader reader = new StringReader(fileInfo[0].ToString());
EnemyList enemies = serializer.Deserialize(reader) as EnemyList;
reader.Close();
return enemies;
}
但是当我开始游戏时,我得到了错误:XmlException:根级别的数据无效。第 1 行,位置 1。我也尝试过 File.ReadAllText(path) 之类的东西,但我得到了“未授权访问异常”。当我用谷歌搜索这个问题时,我发现我需要在路径中指定一个文件而不是目录(不仅仅是 Mods/entities,而是 Mods/entities/entities.xml),但我不想只读取一个文件。我想准备好其中的每个 xml 文件。即使我将其更改为entities.xml,我仍然收到错误IOException:错误267(找不到任何解决方法)
我希望有人可以帮助我。我已经用谷歌搜索了,但论坛上的人做了完全不同的事情,我无法将其应用于我的案例。
先感谢您!
如果需要xml:
解决方案
首先,您需要获取要在其中搜索的目录的路径,然后遍历该目录中的每个 XML 文件。使用 SearchOption.AllDirectory,您还可以搜索当前目录中的所有子目录。
循环浏览您的目录:
string folderWithModFiles = "YourPath";
List<Enemy> enemylist = new List<Enemy>();
foreach (string xmlFile in Directory.EnumerateFiles(folderWithModFiles, "*.xml", SearchOption.AllDirectories)) {
try {
enemylist = SerializeXML(xmlFile);
}
catch(Exception e) {
Debug.LogException(e);
}
}
try catch 是为了确保没有读取错误的 XML 文件。
反序列化 XML 文件:
private List<Enemy> SerializeXMLS(string filePath) {
using (FileStream fileStream = System.IO.File.Open(filePath, FileMode.Open)) {
var serializer = new XmlSerializer(typeof(EnemyList));
var enemyList = serializer.Deserialize(fileStream) as EnemyList;
// Read the data you need from the XML File
// If the user didn't specify certain things he need to specify,
// you can throw an ArgumentExpection and the file will be skipped
return enemyList.ListOfEnemies;
}
}
}
EnemyList XML 解析文件示例:
[XmlRoot("Entities")]
[CreateAssetMenu(menuName = "Nations of Cubion/Item/List of Enemies")]
public class EnemyList {
[XmlArray]
[XmlArrayItem("Entity")]
public List<Entity> ListOfEnemies {
get; set;
}
}
public class Entity{
[XmlAttribute("name")]
public string name{
get; set;
}
[XmlElement("mesh")]
public string mesh {
get; set;
}
[XmlElement("material")]
public string material {
get; set;
}
[XmlElement("enemyType")]
public string enemyType {
get; set;
}
[XmlElement("elementType")]
public string elementType {
get; set;
}
[XmlElement("maxHealth")]
public string maxHealth {
get; set;
}
}
推荐阅读
- pine-script - PineScript:如何只获得前一天的 OHLC 而不是前一天的 OHLC 系列?
- css - 如何在引导网格中将 *ngFor 生成的内容居中?
- onclick - MarkitUp Onclick 事件
- php - 尝试访问 int 类型值的数组偏移量 { DefaultValueBinder.php 第 82 行}
- node.js - Socket.io 如何离开房间
- c++ - 错误:对成员“show”的请求在 Qt 中不明确
- java - Couchbase SDK 3.0 从 java 启用/禁用刷新选项
- c - 在c中用两行声明并初始化一个指向函数的指针数组
- arrays - 按字母顺序排列数组
- typescript - 输入“承诺”
' 不能分配给 Cypress 中的类型 'CanReturnChainable'