none
C# - Reading Specific Data from a File RRS feed

  • Question

  • Hello, I'm having trouble reading specific data from a file and assigning it into a list

    I am trying to read the data inside the speech marks, anyone know can I read the data that is inside of the speech marks only.

    Tuesday, July 7, 2015 8:26 AM

Answers

  • var files = File.ReadLines(@"data\common\country_data\United Kingdom.txt");

    foreach (var f in files) {

    string s = f.Replace("\"", "").Split('=')[1];

    mainGameCountrySelectedPopulationValueLabel.Text = s; }

    How can I do this for each line in the file, as the currently the string s only displays the last line

    Your example here is looping around every line. But it overwrites mainGameCountrySelectedPopulationValueLabel.Text with the extracted value in s every single time. So it ends up just showing the last value.

    Do you actually want the values for each other line? Or just want to scan for one specific line?

    For example, in the loop you can check to see what your variable f starts with to know which line you are currently on and act appropriately:

    var files = File.ReadLines(@"data\common\country_data\United Kingdom.txt");
    foreach (var f in files)
    {
    	if (f.StartsWith("population"))
    	{
    	  string s = f.Replace("\"", "").Split('=')[1];
    	  mainGameCountrySelectedPopulationValueLabel.Text = s;
    	}
    }

    Tuesday, July 7, 2015 11:23 AM

All replies

  • You should read the file line by line. Then look for the index of the first and the second quotation mark. With this indices you can extract the substring. Maybe this example helps:

                var valueString = "MyValue = \"value\"";
    
                var firstIndex = valueString.IndexOf('"');
                var lastIndex = valueString.LastIndexOf('"');
    
                var length = lastIndex - firstIndex;
                var value = valueString.Substring(firstIndex + 1, length - 1);
    


    ---------------------------------- Robin Sedlaczek @ Microsoft Forums

    Tuesday, July 7, 2015 9:24 AM
  • Check this example, that will replace " with empty string and split the line value by the = and read the value of index 1:

    var files = File.ReadLines(@"Yourpathtothe file\File.txt") ;
     foreach (var f in files)
     {
        string s = f.Replace("\"", "").Split('=')[1];
    }


    Fouad Roumieh

    Tuesday, July 7, 2015 9:59 AM
  • var files = File.ReadLines(@"data\common\country_data\United Kingdom.txt");

    foreach (var f in files) {

    string s = f.Replace("\"", "").Split('=')[1];

    mainGameCountrySelectedPopulationValueLabel.Text = s; }

    How can I do this for each line in the file, as the currently the string s only displays the last line
    Tuesday, July 7, 2015 10:24 AM

  • How can I do this for each line in the file, as the currently the string s only displays the last line

    It depends on how you want to show this, do you want to add them all to the label separated each value on a line or what? This how to concatenate for example:

    mainGameCountrySelectedPopulationValueLabel.Text = mainGameCountrySelectedPopulationValueLabel.Text + " " + s;



    Fouad Roumieh

    Tuesday, July 7, 2015 11:15 AM
  • var files = File.ReadLines(@"data\common\country_data\United Kingdom.txt");

    foreach (var f in files) {

    string s = f.Replace("\"", "").Split('=')[1];

    mainGameCountrySelectedPopulationValueLabel.Text = s; }

    How can I do this for each line in the file, as the currently the string s only displays the last line

    Your example here is looping around every line. But it overwrites mainGameCountrySelectedPopulationValueLabel.Text with the extracted value in s every single time. So it ends up just showing the last value.

    Do you actually want the values for each other line? Or just want to scan for one specific line?

    For example, in the loop you can check to see what your variable f starts with to know which line you are currently on and act appropriately:

    var files = File.ReadLines(@"data\common\country_data\United Kingdom.txt");
    foreach (var f in files)
    {
    	if (f.StartsWith("population"))
    	{
    	  string s = f.Replace("\"", "").Split('=')[1];
    	  mainGameCountrySelectedPopulationValueLabel.Text = s;
    	}
    }

    Tuesday, July 7, 2015 11:23 AM
  • I would like a label to display each value of each line in the text file
    Tuesday, July 7, 2015 11:47 AM
  • I would like a label to display each value of each line in the text file
    Tuesday, July 7, 2015 11:47 AM
  • I would like a label to display each value of each line in the text file

    Yes but what is the format that you want?

    For example set the Label AutoSize property to False, set its width and height. The below loop will allow you to read each value and set it on a seperate line using \n

    var files = File.ReadLines(@"Yourpath\File.txt");
    
                foreach (var f in files)
                {
                    string s = f.Replace("\"", "").Split('=')[1];
                    label1.Text =label1.Text + "\n" + s;
                }


    Fouad Roumieh


    Tuesday, July 7, 2015 11:58 AM
  • I would like the label containing the value to automatically change it size depending length of the data and I would like for each line to be in a separate label.

    Example:

    Tuesday, July 7, 2015 12:31 PM
  • Check the below loop, that will read the title and the value:

    var files = File.ReadLines(@"C:\Documents and Settings\Luis\Desktop\File.txt");
    
                foreach (var f in files)
                {
                    string title = f.Replace("\"", "").Split('=')[0];
                    string s = f.Replace("\"", "").Split('=')[1];
                    label1.Text = label1.Text + "\n" + title + ": " + s;
                }

     if you want the titles different to what is in the file you have to do replace.

    You should have provided your requirements clear form the beginning, we cannot elaborate with each post. 


    Fouad Roumieh


    Tuesday, July 7, 2015 12:41 PM
  • I don't want the titles be displayed programmatically, I want the values to be displayed but not in one label, I want one label for population, one label for job title etc. 
    Tuesday, July 7, 2015 12:51 PM
  • var files = File.ReadLines(@"data\common\country_data\United Kingdom.txt");
                foreach (var f in files)
                {
                    if (f.StartsWith("population"))
                    {
                        string s = f.Replace("\"", "").Split('=')[1];
                        mainGameCountrySelectedPopulationValueLabel.Text = s;
                    }
    
                    if (f.StartsWith("job_title"))
                    {
                        string s = f.Replace("\"", "").Split('=')[1];
                        mainGameCountrySelectedJobTitleValueLabel.Text = s;
                    }
    
                    if (f.StartsWith("forename"))
                    {
                        string s = f.Replace("\"", "").Split('=')[1];
                        mainGameCountrySelectedForenameValueLabel.Text = s;
                    }
    
                    if (f.StartsWith("surname"))
                    {
                        string s = f.Replace("\"", "").Split('=')[1];
                        mainGameCountrySelectedSurnameValueLabel.Text = s;
                    }
    
                    if (f.StartsWith("political_party"))
                    {
                        string s = f.Replace("\"", "").Split('=')[1];
                        mainGameCountrySelectedPoliticalPartyValueLabel.Text = s;
                    }
    This works but is this the most efficient way of doing this
    Tuesday, July 7, 2015 1:00 PM