2014-03-26 198 views
2

我在一个文件夹中有大约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> 

一个批处理文件无法正常工作;你可以在我的其他线程看到:Batch script not working?

我需要一个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#控制台应用程序中做到这一点。

我在寻求帮助,任何感激!

+0

你可能寻找'Directory.GetFiles'方法在http://msdn.microsoft.com/en-us/library/wz42302f(v=vs看到。 110)的.aspx。如果你正在处理多层次的目录,你可能需要获得目标文件夹的列表,并递归调用你的处理函数。 –

+0

我没有处理多层次的目录,它们都在一个文件夹中。 – user3457618

+0

'Directory.GetFiles'让你把所有的文件放在一个特定的目录中 - 你需要它,相信他;-) –

回答

2

这是我写的一个快速而脏的程序。我不得不为你的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(); 
     } 
    } 
} 
+0

完美无缺。我只需在循环中添加以读取所有xmls,并将其保存为.txt – user3457618

2
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); 
+0

但是它们是XML文件?另外你的意思是将它存储在importantLines中,我将如何存储它?但是这些都是XML文件,如果你把这些文件放到一个字符串中,我的代码就无法工作了。 – user3457618

+0

XML文件也是纯文本,对不对?所以你可以打开每个文件(这是由File.ReadAllLines完成),并只处理文件中的信息(这必须在注释所在的位置完成)。你只需在“完整”文本文件中追加你想要的行。 –

+0

雅,但我发布的代码是XELEMENT,我不知道如何做到这一点,如果它不是一个XML文件... – user3457618

相关问题