简体   繁体   中英

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

I have a textile contains big amount of data ,first thing is i have to filter Leaf cell data which is scattered over there and here.For this first line i filtered is line beginning with ADD GCELL which contains primary data,next what i have to do is i have to get the related data from the same text file by using CELLID coming in the same ADD GCELL line.Related datas are coming in the line beggining with ADD GTRX and datas are FREQ , TRXNO , ISMAINBCCH , .in nutshell CELLID is the common value for both line ADD GCELL and ADD GTRX . I have done few coding in c# , but i got stuck somewhere Here is 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 i have done is

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 one more condition i have to satisfy.Here for for ADD Gtrx: i am taking all values including Freq when ISMAINBCCH=YES ,but at the same time ISMAINBCCH=NO there are values for Freq which i have to get as comma seperated values.For example Like here First i will take FREQ where CELLID = 639(dynamic one anything can happen) and ISMAINBCCH=YES,that i have done now next task is i have to contenate FREQ values in a comma seperated way where CELLID=639 and ISMAINBCCH=NO, so here the output i want is 24,28,67 .How to achieve this 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 i did it like shown below code

i created one more property DEFINED_TCH_FRQ = null for getting concatenated string.But the problem is it is very slow .I am iterating text file two times ,first time is sr.readline() and second is for getting concatenated string by File.Readline (this aslo previously i used File.Readalllines and 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 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

Finally i did it like first i saved lines starting with ADD GTRX in to an array by using File.Readalllines and then used only that array to get concatenated string instead of storing entire text file and got some performance improvement.Now my question is if i convert my Text files each contain hundreds of thousands of lines in to xml and then retrieve data from xml file, will it make any performance improvement? if i use datatable and dataset rather than classes here will it make any performance improvement?

Assuming the data is consistent and I'm also assuming the GCells will come before GTrx line (since GTrx is referencing the id of the GCell), then you could create a simple parser for doing this and store the values in a dictionary.

First thing to do is create a class to hold the Gtrx data and the GCell data. Keep in mind that I am just grabbing a subset of the data. You can add to this if you 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 to these classes, we will also need a class to "link" these two classes together:

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

Now we can build a 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 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
// }

And finally, we need to define a 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 do it! See if that doesn't work for you. Keep in mind that this is very basic. You'd probably want to handle some possible errors (such as the key not existing, etc).

You didn't specify what exactly is going wrong, but my guess is that the problem you are having is caused by your split:

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

This split causes your strings to be " CELLID" and "13" (from your file example). Notice the space in front of "CELLID". This causes the following code to never pass:

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

You could change it to:

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

it might work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM