.net - How to get data's from text file in c# -


i have textile contains big amount of data ,first thing have filter leaf cell data scattered on there , here.for first line filtered line beginning add gcell contains primary data,next have have related data same text file using cellid coming in same add gcell line.related datas coming in line beggining add gtrx , datas freq , trxno , ismainbcch ,.in nutshell cellid common value both line add gcell , add gtrx. have done few coding in c# , got stuck somewhere here part of text file ........................... ...........................

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; 

code have done

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

everything working fine except 1 more condition have satisfy.here for add gtrx: taking values including freq when ismainbcch=yes ,but @ same time ismainbcch=no there values freq have comma seperated values.for example here first take freq cellid = 639(dynamic 1 can happen) , ismainbcch=yes,that have done next task have contenate freq values in comma seperated way cellid=639 , ismainbcch=no, here output want 24,28,67 .how achieve one

lines are

 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

finally did shown below code

i created 1 more property defined_tch_frq = null getting concatenated string.but problem is slow .i iterating text file 2 times ,first time sr.readline() , second getting concatenated string file.readline(this aslo used file.readalllines , got out of memory exception)

 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 id. something!                                     continue;                                 }                                 intarr.add(gtrx.cellid);                                 string results = string.empty;                                      var result = string.join(",",         ss in file.readlines(filename)         ss.contains("add gtrx:")         int.parse(pullvalue(ss, "cellid")) == gtrx.cellid         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

finally did first saved lines starting add gtrx in array using file.readalllines , used array concatenated string instead of storing entire text file , got performance improvement.now question if convert text files each contain hundreds of thousands of lines in xml , retrieve data xml file, make performance improvement? if use datatable , dataset rather classes here make performance improvement?

assuming data consistent , i'm assuming gcells come before gtrx line (since gtrx referencing id of gcell), create simple parser doing , store values in dictionary.

first thing create class hold gtrx data , gcell data. keep in mind grabbing subset of data. can add if need more fields:

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; } } 

in addition these classes, need class "link" these 2 classes together:

private class gcellgtrx {     public gcell gcell { get; set; }     public gtrx gtrx { get; set; } } 

now can build simple parser:

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 id. something!                 continue;             }             _dictionary[gtrx.cellid].gtrx = gtrx;         }         line = sr.readline();     } }  // can pull data using cellid: // gcellgtrx cell13 = _dictionary[13]; //  // or iterate through each one: // foreach (keyvaluepair<int, gcellgtrx> kvp in _dictionary) // { //     int key = kvp.key; //     gcellgtrx gcellgtrxdata = kvp.value; //     // stuff // } 

and finally, need define simple helper method:

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 ""; } 

that should it! see if doesn't work you. keep in mind basic. you'd want handle possible errors (such key not existing, etc).


Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -