将XML文件转化成JSON文件

[MenuItem("ExportXmlLanguage/将ChinaData下的XML文件转成Json文件")]

public static void ReadFiles() {

string path = Application.dataPath + "/AlllanguageResources/ChinaData";

DirectoryInfo sourceFolder = new DirectoryInfo(path);

if (sourceFolder != null) {

FileInfo[] fileInfoArray = sourceFolder.GetFiles();       

foreach (FileInfo file in fileInfoArray) {

if (file.Name.EndsWith(".xml")) {

int index = file.Name.IndexOf('.');

ReadXmlFile(path + "/" + file.Name, file.Name.Substring(0, index));

}

}

EditorUtility.DisplayDialog("转换完成", "请在ChinaJsonData目录下查看Json文件", "OK");

}

}


private static void ReadXmlFile(string xmlPath, string fileName) {

XmlDocument xmlDoc = new XmlDocument();

xmlDoc.Load(xmlPath);

string json = JsonConvert.SerializeXmlNode(xmlDoc);

SaveJsonFile(json, fileName);

}


private static void SaveJsonFile(string json, string fileName) {

string path = Application.dataPath + "/AlllanguageResources/ChinaJsonData";

string filePath = path + "/" + fileName + ".txt";

if (!Directory.Exists(path)) {

Directory.CreateDirectory(path);

}

if (File.Exists(filePath)) {

File.Delete(filePath);

}

FileStream fileStream = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);

fileStream.Position = fileStream.Length;

StreamWriter writer = new StreamWriter(fileStream);

writer.AutoFlush = true;

writer.WriteLine(json);

writer.Flush();

writer.Close();

fileStream.Close();

AssetDatabase.Refresh();

}

备注:路径需要根据项目实际情况分配。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,981评论 19 139
  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 9,505评论 0 13
  • •王越:VMware存储API整理,比如核心的存储池与卷两者的CURD。可以参考:VMware in OpenSt...
    笨手笨脚越阅读 5,718评论 0 2
  • This project was bootstrapped with Create React App. Belo...
    unspecx阅读 5,226评论 0 2
  • 这大概就是所谓的虚荣? 完全不能理解意义何在。 必须要把计划提上日程。 终结这个扭曲的关系了。
    沃雷塔尔阅读 188评论 0 0