2014-03-26 102 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

相關問題