From 70356bcd3659cbb6f8218f8f0d876cf82a60f431 Mon Sep 17 00:00:00 2001 From: m0_75251201 Date: Sat, 12 Jul 2025 11:53:21 +0800 Subject: [PATCH] =?UTF-8?q?(client)=20feat:=E6=B7=BB=E5=8A=A0=E5=B0=86XML?= =?UTF-8?q?=E8=AF=BB=E5=8F=96=E4=B8=BAXML=E7=BB=84=E4=BB=B6=E7=9A=84?= =?UTF-8?q?=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Client/Assets/Scripts/Data/DefineManager.cs | 2 +- Client/Assets/Scripts/Utils/FileHandler.cs | 36 ++++++++++++++++++++- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/Client/Assets/Scripts/Data/DefineManager.cs b/Client/Assets/Scripts/Data/DefineManager.cs index 3e26eb7..80c1422 100644 --- a/Client/Assets/Scripts/Data/DefineManager.cs +++ b/Client/Assets/Scripts/Data/DefineManager.cs @@ -19,7 +19,7 @@ namespace Data public void Init() { // 加载所有 XML 文件路径 - var xmlFilePaths = FileHandler.LoadXmlFilesFromPaths(dataSetFilePath); + var xmlFilePaths = FileHandler.GetXmlFilePathsFromPaths(dataSetFilePath); // 遍历并加载每个 XML 文件 foreach (var xmlFilePath in xmlFilePaths) diff --git a/Client/Assets/Scripts/Utils/FileHandler.cs b/Client/Assets/Scripts/Utils/FileHandler.cs index 3e97b41..ac0ee43 100644 --- a/Client/Assets/Scripts/Utils/FileHandler.cs +++ b/Client/Assets/Scripts/Utils/FileHandler.cs @@ -1,8 +1,10 @@ using System; using System.Collections.Generic; using System.IO; +using System.Xml; using Newtonsoft.Json; using UnityEngine; +using Formatting = Newtonsoft.Json.Formatting; namespace Utils { @@ -158,7 +160,7 @@ namespace Utils /// /// 文件夹路径 /// - public static List LoadXmlFilesFromPaths(string[] paths) + public static List GetXmlFilePathsFromPaths(string[] paths) { var xmlFilePaths = new List(); @@ -194,6 +196,38 @@ namespace Utils return xmlFilePaths; } + + /// + /// 从指定路径加载所有 XML 文件并解析为 XmlDocument 对象。 + /// + /// 文件夹路径数组。 + /// 包含所有解析后的 XmlDocument 对象的列表。 + public static List LoadXmlFromPaths(string[] paths) + { + var xmlDocuments = new List(); + var xmlFilePaths = GetXmlFilePathsFromPaths(paths); + + foreach (var filePath in xmlFilePaths) + { + try + { + // 创建一个新的 XmlDocument 实例 + var xmlDoc = new XmlDocument(); + + // 加载 XML 文件内容 + xmlDoc.Load(filePath); + + // 将解析后的 XmlDocument 添加到结果列表中 + xmlDocuments.Add(xmlDoc); + } + catch (Exception ex) + { + Console.Error.WriteLine($"加载 XML 文件 {filePath} 时发生错误: {ex.Message}"); + } + } + + return xmlDocuments; + } } } \ No newline at end of file