2012-05-09 39 views
0

我在文件中下面的文字現在最佳C#文件類解析文本文件

Index: D:/QATV2Demo/Main.msbuild 
=================================================================== 
--- D:/QATV2Demo/Main.msbuild (revision 12414) 
+++ D:/QATV2Demo/Main.msbuild (revision 12416) 
--- D:/QATV2Demo/Main.msbuild (revision 12414) 
+++ D:/QATV2Demo/Main.msbuild (revision 12416) 
@@ -39,7 +39,7 @@ 
    AssemblyFile="$(ToolsBinPath)\MSBuild.Community.Tasks.dll" /> 

- <FxCop_CriticalErrors>10</FxCop_CriticalErrors> 
+ <FxCop_CriticalErrors>0</FxCop_CriticalErrors> 


<FxCop_Errors>0</FxCop_Errors> 
<FxCop_CriticalWarnings>0</FxCop_CriticalWarnings> 
<FxCop_Warnings>0</FxCop_Warnings> 


    Index: D:/QATV2Demo/QATV2Demo/QATConstant.cs 
=================================================================== 
--- D:/QATV2Demo/QATV2Demo/QATConstant.cs (revision 12414) 
+++ D:/QATV2Demo/QATV2Demo/QATConstant.cs (revision 12416) 
@@ -9,7 +9,7 @@ 
    { 
    public static readonly string PAGE_DATA_DROP_DOWN_MODE = "D"; 
    public static readonly string PAGE_DATA_GRID_MODE = "G"; 
-  public static readonly string REPORT = "Report"; 
+  public static readonly string REPORT = "Report1"; 
    public static readonly string ITEM_COUNT = "ItemCount"; 
} 

}

我已經寫我自己的代碼,讓我產生這是該行以 - 和+ 開頭,顯示文件的內容差異。

這裏是我的代碼

int counter = 0; 
     string line; 
     string filename = args[0].ToString(); 


     using (System.IO.StreamReader file = new StreamReader(filename)) 
     { 
      while ((line = file.ReadLine()) != null) 
      { 
       if (line.StartsWith("- ")||line.StartsWith("+ ")) 
       { 
        Console.WriteLine(line.Trim('-',' ','+')); 

       } 
       counter++; 
      } 

      file.Close(); 
     } 
     // Suspend the screen. 
     Console.WriteLine(counter); 

這是我用圍脖代碼。

請告訴我哪點網類是最適合我在這種情況下提前

感謝。

回答

1

如果代碼做到了你想要的,那麼就沒有必要改變解析。您可以通過使用File.ReadLines,這樣簡化了文件閱讀:

foreach (var line in File.ReadLines(filename)) 
{ 
    if (line.StartsWith("- ") || line.StartsWith("+ ")) 
    { 
     // do stuff here 
    } 
    ++counter; 
} 

除此之外,你不是說你想用添加和刪除線做,一旦你發現他們什麼。所以我不能在那裏提供任何建議。

+0

我只是想澄清一點,那裏有沒有其他的類可能更好。以及如何將這些內容寫入XML文件( 10) –