2015-05-29 110 views
0

我有一個紡織品包含大量的數據,第一件事是我必須過濾葉細胞數據,這是分散在那裏和here.For第一行我過濾行是開始於ADD GCELL其中包含原始數據,接下來我要做的是我必須通過使用CELLID來獲取相同的文本文件中的相關數據進來相同的ADD GCELL線。相關的數據來自ADD GTRX開始的行和數據是FREQ , TRXNO , ISMAINBCCH ,。簡而言之,CELLID是行ADD GCELLADD GTRX的常見值。我已經在c#中完成了一些編碼,但我被卡住了某處 這是文本文件 ........................... 的一部分。 ..........................如何從c#中的文本文件獲取數據

ADD GCELL:CELLID=13, CELLNAME="NR_0702_07021_G1_A", MCC="424", MNC="02", LAC=6112, CI=7021, NCC=6, BCC=0, EXTTP=Normal_cell, IUOTP=Concentric_cell, ENIUO=ON, DBFREQBCCHIUO=Extra, FLEXMAIO=OFF, CSVSP=3, CSDSP=5, PSHPSP=4, PSLPSVP=6, BSPBCCHBLKS=1, BSPAGBLKSRES=4, BSPRACHBLKS=1, TYPE=GSM900_DCS1800, OPNAME="Tester", VIPCELL=NO 
.............................. 
ADD GTRX:TRXID=11140, TRXNAME="T_RAK_JaziratHamra_G_702_7021_A-0", FREQ=99, TRXNO=0, CELLID=13, IDTYPE=BYID, ISMAINBCCH=YES, ISTMPTRX=NO, GTRXGROUPID=80; 

代碼,我所做的就是

using (StreamReader sr = File.OpenText(filename)) 
{ 
    while ((s = sr.ReadLine()) != null) 
    { 
     if (s.Contains("ADD GCELL:")) 
     { 
      s = s.Replace("ADD GCELL:", ""); 
      string[] items = s.Split(','); 
      foreach (string str in items) 
      { 
       string[] str1 = str.Split('='); 
       if (str1[0] == "CELLID") 
       { 
        cellidnew = str1[1]; 
       } 
       string fieldname = str1[0]; 
       string value = str1[1].Replace(";", string.Empty).Replace("\"", string.Empty); 

      } 

      Getgtrxvalues(filename, ref cellname, ref cellidnew, ref Frequency, ref TRXNO ,ref ISMAINBCCH); 


     } 
    } 
} 

private static void Getgtrxvalues(string filename, ref string cellname, ref string cellid, ref int Frequency, ref int TRXNO ,ref bool ISMAINBCCH) 
{ 
    using (StreamReader sr = File.OpenText(filename)) 
    { 
     while ((s = sr.ReadLine()) != null) 
     { 
      if (s.Contains("ADD GTRX:")) 
      { 
       try 
       { 


} 
} 
} 
} 

UPDATE

一切工作正常,除了一個更多的條件,我必須滿足。在這裏爲ADD Gtrx:我將所有的價值包括Freq當ISMAINBCCH = YES,但同時ISMAINBCCH = NO有Freq值,我必須得到逗號分隔值。例如像這裏首先,我將採取FREQ CELLID = 639(動態的任何事情可能發生)和ISMAINBCCH =是的,我現在已經完成了下一個任務是我必須以CELLID = 639和ISMAINBCCH = NO的逗號分隔方式來使用FREQ值,所以這裏我想要的輸出是24,28,67。如何實現這個

ADD GTRX:TRXID=0, TRXNAME="M_RAK_JeerExch_G_1879_18791_A-0", FREQ=81, TRXNO=0, CELLID=639, IDTYPE=BYID, ISMAINBCCH=YES, ISTMPTRX=NO, GTRXGROUPID=2556; 
ADD GTRX:TRXID=1, TRXNAME="M_RAK_JeerExch_G_1879_18791_A-1", FREQ=24, TRXNO=1, CELLID=639, IDTYPE=BYID, ISMAINBCCH=NO, ISTMPTRX=NO, GTRXGROUPID=2556; 
ADD GTRX:TRXID=5, TRXNAME="M_RAK_JeerExch_G_1879_18791_A-2", FREQ=28, TRXNO=2, CELLID=639, IDTYPE=BYID, ISMAINBCCH=NO, ISTMPTRX=NO, GTRXGROUPID=2556; 
ADD GTRX:TRXID=6, TRXNAME="M_RAK_JeerExch_G_1879_18791_A-3", FREQ=67, TRXNO=3, CELLID=639, IDTYPE=BYID, ISMAINBCCH=NO, ISTMPTRX=NO, GTRXGROUPID=2556; 

UPDATE

最後我做到了像如下所示代碼

我創建了一個更多的屬性DEFINED_TCH_FRQ = null獲得連接字符串。但問題是它非常緩慢。我迭代文本文件兩次,第一次是sr.readline(),第二個是獲取連接字符串通過File.Readline(這ASLO先前我用File.Readalllines,拿出存儲器異常)

List<int> intarr = new List<int>(); 
      intarr.Clear(); 
var gtrx = new Gtrx 
          { 
           CellId = int.Parse(PullValue(s, "CELLID")), 
           Freq = int.Parse(PullValue(s, "FREQ")), 
           TrxNo = int.Parse(PullValue(s, "TRXNO")), 
           IsMainBcch = PullValue(s, "ISMAINBCCH").ToUpper() == "YES", 
           Commabcch = new List<string> { PullValue(s, "ISMAINBCCH") }, 
           DEFINED_TCH_FRQ = null, 

           TrxName = PullValue(s, "TRXNAME"), 

          }; 

if (!intarr.Contains(gtrx.CellId)) 
          { 

           if (!_dictionary.ContainsKey(gtrx.CellId)) 
           { 
            // No GCell record for this id. Do something! 
            continue; 
           } 
           intarr.Add(gtrx.CellId); 
           string results = string.Empty; 

            var result = String.Join(",", 
     from ss in File.ReadLines(filename) 
     where ss.Contains("ADD GTRX:") 
     where int.Parse(PullValue(ss, "CELLID")) == gtrx.CellId 
     where PullValue(ss, "ISMAINBCCH").ToUpper() != "YES" 
     select int.Parse(PullValue(ss, "FREQ"))); 
            results = result; 


           var gtrxnew = new Gtrx 
           { 
            DEFINED_TCH_FRQ = results 
           }; 

           _dictionary[gtrx.CellId].Gtrx = gtrx; 

UPDATE

最後我沒有像第一i-通過使用文件保存在一個數組起始ADD GTRX線.Readal然後使用該數組來獲取連接字符串,而不是存儲整個文本文件,並獲得了一些性能改進。現在我的問題是,如果我將我的文本文件中每個都包含數十萬行到xml,然後從xml文件中檢索數據,它會改善性能嗎?如果我在這裏使用數據表和數據集而不是類,它會使性能得到改善嗎?

+0

這聽起來像是正則表達式的工作。 –

+0

我需要一個代碼來獲得剔除功能,我已經完成了部分編碼,但需要完成它。目標是獲得行ADD GOLD的所有值和ADD GTRX行的相應值 – peter

+0

我一直在寫40多個文本解析器如果沒有看到完整的輸入數據樣本,您無法修復這些類型的問題。每個文本文件都完全不同,並且無需瞭解文本的完整結構就無法幫助您。見我的解析器之一在以下網頁:http://stackoverflow.com/questions/30457660/parsing-multi-sections-of-a-text-file-using-regex-in-c-sharp/30458775#30458775 – jdweng

回答

2

假設數據是一致的,我也假定GCells會在GTrx行之前(因爲GTrx引用GCell的id),那麼你可以創建一個簡單的解析器來做到這一點,並將值存儲在字典。

首先要做的就是創建一個類來保存Gtrx數據和GCell數據。請記住,我只是抓住了一部分數據。如果你需要更多的字段可以添加到這個:

private class Gtrx 
{ 
    public int Freq { get; set; } 
    public int TrxNo { get; set; } 
    public string TrxName { get; set; } 
    public int CellId { get; set; } 
    public bool IsMainBcch { get; set; } 
} 

private class Gcell 
{ 
    public int CellId { get; set; } 
    public string CellName { get; set; } 
    public string Mcc { get; set; } 
    public int Lac { get; set; } 
    public int Ci { get; set; } 
} 

除了這些課程,我們還需要一個類來「鏈接」這兩個類一起:

private class GcellGtrx 
{ 
    public Gcell Gcell { get; set; } 
    public Gtrx Gtrx { get; set; } 
} 

現在我們可以建立一個簡單的解析器:

private readonly Dictionary<int, GcellGtrx> _dictionary = new Dictionary<int, GcellGtrx>(); 

string data = "ADD GCELL:CELLID=13, CELLNAME=\"NR_0702_07021_G1_A\", MCC=\"424\", MNC=\"02\", LAC=6112, CI=7021, NCC=6, BCC=0, EXTTP=Normal_cell, IUOTP=Concentric_cell, ENIUO=ON, DBFREQBCCHIUO=Extra, FLEXMAIO=OFF, CSVSP=3, CSDSP=5, PSHPSP=4, PSLPSVP=6, BSPBCCHBLKS=1, BSPAGBLKSRES=4, BSPRACHBLKS=1, TYPE=GSM900_DCS1800, OPNAME=\"Tester\", VIPCELL=NO" + Environment.NewLine; 
data = data + "ADD GTRX:TRXID=11140, TRXNAME=\"T_RAK_JaziratHamra_G_702_7021_A-0\", FREQ=99, TRXNO=0, CELLID=13, IDTYPE=BYID, ISMAINBCCH=YES, ISTMPTRX=NO, GTRXGROUPID=80;" + Environment.NewLine; 

using (var sr = new StringReader(data)) 
{ 
    string line = sr.ReadLine(); 
    while (line != null) 
    { 
     line = line.Trim(); 
     if (line.StartsWith("ADD GCELL:")) 
     { 
      var gcell = new Gcell 
      { 
       CellId = int.Parse(PullValue(line, "CELLID")), 
       CellName = PullValue(line, "CELLNAME"), 
       Ci = int.Parse(PullValue(line, "CI")), 
       Lac = int.Parse(PullValue(line, "LAC")), 
       Mcc = PullValue(line, "MCC") 
      }; 
      var gcellGtrx = new GcellGtrx(); 
      gcellGtrx.Gcell = gcell; 
      _dictionary.Add(gcell.CellId, gcellGtrx); 
     } 
     if (line.StartsWith("ADD GTRX:")) 
     { 
      var gtrx = new Gtrx 
      { 
       CellId = int.Parse(PullValue(line, "CELLID")), 
       Freq = int.Parse(PullValue(line, "FREQ")), 
       TrxNo = int.Parse(PullValue(line, "TRXNO")), 
       IsMainBcch = PullValue(line, "ISMAINBCCH").ToUpper() == "YES", 
       TrxName = PullValue(line, "TRXNAME") 
      }; 

      if (!_dictionary.ContainsKey(gtrx.CellId)) 
      { 
       // No GCell record for this id. Do something! 
       continue; 
      } 
      _dictionary[gtrx.CellId].Gtrx = gtrx; 
     } 
     line = sr.ReadLine(); 
    } 
} 

// Now you can pull your data using a CellId: 
// GcellGtrx cell13 = _dictionary[13]; 
// 
// Or you could iterate through each one: 
// foreach (KeyValuePair<int, GcellGtrx> kvp in _dictionary) 
// { 
//  int key = kvp.Key; 
//  GcellGtrx gCellGtrxdata = kvp.Value; 
//  // Do Stuff 
// } 

最後,我們需要定義一個簡單的輔助方法:

private string PullValue(string line, string key) 
{ 
    key = key + "="; 
    int ndx = line.IndexOf(key, 0, StringComparison.InvariantCultureIgnoreCase); 
    if (ndx >= 0) 
    { 
     int ndx2 = line.IndexOf(",", ndx, StringComparison.InvariantCultureIgnoreCase); 
     if (ndx2 == -1) 
      ndx2 = line.Length - 1; 
     return line.Substring(ndx + key.Length, ndx2 - ndx - key.Length).Trim('"').Trim(); 
    } 

    return ""; 
} 

應該這樣做!看看這不適合你。請記住,這是非常基本的。你可能想要處理一些可能的錯誤(如不存在的關鍵等)。

+0

獲得相應的值,也許看起來我們是如何發送多個車型在MVC使用視圖模型,兩個階級,一個超集CLAS – peter

+1

呀,個人,對我來說,我喜歡一切都打包成一個類來查看。它使我更容易使用程序。 – Icemanind

+1

@peter - 添加一個例子,你的問題 – Icemanind

0

沒有指定究竟是哪裏錯了,但我的猜測是,你所遇到的問題是你的分裂造成的:

string[] str1 = str.Split('='); 

這種分裂會導致你的字符串是「CELLID」和「 13「(來自您的文件示例)。注意「CELLID」前面的空格。這會導致下面的代碼永遠不會傳遞:

if (str1[0] == "CELLID") 

你可以將其更改爲:

if (str1[0].Trim() == "CELLID") 

它可能工作。

+0

我已經爲我完成了,我需要一個代碼來獲得剔除功能,我已經完成了部分編碼,但需要完成它。目標是從行ADD GCELL獲取所有值,並從ADD GTRX行 – peter

相關問題