循环遍历目录中的每个文件并将输出写入文本

本文关键字:输出 文本 文件 遍历 循环 | 更新日期: 2023-09-27 17:53:49

我在一个文件夹中有大约15,000个XML文件,XML文件名的一个例子是;000010000. img.xml

每个XML文件都包含我需要的特定信息。

每个XML文件除了所表示的信息外,具有完全相同的结构。

这是我想要关注的(在XML文件中)

<imgdir name="000010000.img">
   <imgdir name="info">
      <int name="version" value="10" />
      <int name="cloud" value="0" />
      <int name="town" value="0" />
      <float name="mobRate" value="1.0" />
      <string name="bgm" value="Bgm34/MapleLeaf" />
      <int name="returnMap" value="10000" />
      <string name="mapDesc" value="" />
      <int name="hideMinimap" value="0" />
      <int name="forcedReturn" value="999999999" />
      <int name="moveLimit" value="0" />
      <string name="mapMark" value="MushroomVillage" />
      <int name="swim" value="0" />
      <int name="fieldLimit" value="8260" />
      <int name="VRTop" value="-892" />
      <int name="VRLeft" value="-1064" />
      <int name="VRBottom" value="915" />
      <int name="VRRight" value="1334" />
      <int name="fly" value="0" />
      <int name="noMapCmd" value="0" />
      <string name="onFirstUserEnter" value="" />
      <string name="onUserEnter" value="go10000" />
      <int name="standAlone" value="0" />
      <int name="partyStandAlone" value="0" />
      <string name="fieldScript" value="" />
   </imgdir>
   </imgdir>
   <imgdir name="portal">
      <imgdir name="0">
         <string name="pn" value="sp" />
         <int name="pt" value="0" />
         <int name="x" value="-389" />
         <int name="y" value="183" />
         <int name="tm" value="999999999" />
         <string name="tn" value="" />
      </imgdir>
      <imgdir name="1">
         <string name="pn" value="sp" />
         <int name="pt" value="0" />
         <int name="x" value="-416" />
         <int name="y" value="185" />
         <int name="tm" value="999999999" />
         <string name="tn" value="" />
      </imgdir>
      <imgdir name="2">
         <string name="pn" value="sp" />
         <int name="pt" value="0" />
         <int name="x" value="-450" />
         <int name="y" value="183" />
         <int name="tm" value="999999999" />
         <string name="tn" value="" />
      </imgdir>
      <imgdir name="3">
         <string name="pn" value="out00" />
         <int name="pt" value="2" />
         <int name="x" value="1080" />
         <int name="y" value="541" />
         <int name="tm" value="20000" />
         <string name="tn" value="in00" />
         <string name="script" value="" />
         <int name="hideTooltip" value="0" />
         <int name="onlyOnce" value="0" />
         <int name="delay" value="0" />
      </imgdir>
   </imgdir>

批处理文件不工作;正如你可以看到在我的其他线程:批处理脚本不工作?

我需要一个c#应用程序来打开每个XML文件,获取特定的信息(我将在下面指定),将该信息写入单个文本文件,然后冲洗和重复,直到读取每个XML文件。

使用上面发布的XML文件片段/实际XML文件信息,下面是我如何需要文本文件的文本结构;

[10000]
total=4
sp 0 -389 183 999999999
sp 0 -416 185 999999999
sp 0 -450 183 999999999
out00 2 1080 541 20000

我只是不知道如何在c#控制台应用程序中做到这一点。

我正在寻求帮助,任何事情都是感激的!

循环遍历目录中的每个文件并将输出写入文本

string[] files = Directory.GetFiles(@"SomeWhere");
List<string> result = new List<string>();
foreach (string file in files)
{
    string[] lines = File.ReadAllLines(file);
    // Grab information from the lines and store it in result
    // by using result.Add(...) or result.AddRange(...)
}
File.WriteAllLines(@"AlsoSomewhere", result);

这是我编写的一个快速而粗糙的程序。我必须在XML文件中添加一个根,以便它与程序配合得很好。我希望这对你有帮助!

代码:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Linq;
namespace ConsoleApp5 {
    public class Program {
        public static void Main(string[] args) {
            string test = getValuesOneFile("xmltest.xml");
            Console.WriteLine(test);
            Console.ReadLine();
        }
        public static string getValuesOneFile(string fileName) {
            string finalString = "";
            XElement xmlDocument = XElement.Load((fileName));
            XElement infoImgDir = GetImgDir(xmlDocument, "info");
            finalString += "[" + GetInt(infoImgDir, "returnMap") + "]'n";
            finalString += "total=" + GetChildrenCount(GetImgDir(xmlDocument,"portal")) + "'n";
            IEnumerable<XElement> portals = GetImgDir(xmlDocument, "portal").Elements();
            foreach (XElement currentPortal in portals) {
                finalString += GetString(currentPortal, "pn") + " ";
                finalString += GetInt(currentPortal, "pt") + " ";
                finalString += GetInt(currentPortal, "x") + " ";
                finalString += GetInt(currentPortal, "y") + " ";
                finalString += GetInt(currentPortal, "tm") + "'n";
            }
            return finalString;
        }
        public static XElement GetImgDir(XElement file, string imgDirName) {
            return file.Descendants("imgdir").FirstOrDefault(x => (string)x.Attribute("name") == imgDirName);
        }
        public static int GetInt(XElement data, string attribName) {
            var element = data.Descendants("int").FirstOrDefault(x => (string)x.Attribute("name") == attribName);
            if (element == null)
                return 0;
            var value = (int?)element.Attribute("value");
            if (value == null)
                return 0;
            return value.Value;
        }
        public static string GetString(XElement data, string attribName) {
            var element = data.Descendants("string").FirstOrDefault(x => (string)x.Attribute("name") == attribName);
            if (element == null)
                return "";
            var value = (string)element.Attribute("value");
            if (value == null)
                return "";
            return value;
        }
        public static int GetChildrenCount(XElement data) {
            return data.Elements().Count();
        }
    }
}