locked
ASP C# Read Single Line from Text File and read results into Array RRS feed

  • Question

  • User-973788521 posted

    Ok, I have a text file that is created and it has many-many lines. Each line needs to be read into an array and passed to a chart (ChartJS). If I hard code the values into the code behind (.cs) page, it works just fine, but I try to read the file and put the data into an array to feed the chart - things go sideways. 

    Here's why I tried:

    protected string[] Months { get; private set; }
    protected void Page_Load(object sender, EventArgs e)
    {
            if (!Page.User.Identity.IsAuthenticated) FormsAuthentication.RedirectToLoginPage();
    	MasterLoad();
    }
    private void MasterLoad()
    		{
    			GetMonths();
    		}
    [WebMethod]
    public string[] GetMonths()
    {
       var appDataFolder = Path.Combine(HttpContext.Current.Request.PhysicalApplicationPath ?? throw new InvalidOperationException(), "App_Data");
       var fileName = appDataFolder + "\\filename.txt";
       string[] data = File.ReadLines(fileName).Skip(0).Take(1).ToArray();
       var results = new[]
       {
          data.ToString()
       };
       return results;
    }

    And I tried this:

    [WebMethod]
    public void GetMonths()
    	{
             	var appDataFolder = Path.Combine(HttpContext.Current.Request.PhysicalApplicationPath ?? throw new InvalidOperationException(), "App_Data");
    	        var fileName = appDataFolder + "\\14000.txt";
    		var data = File.ReadLines(fileName).Skip(0).Take(1);
    		Months = data as string[];
    	}

    Here's the text file

    "January","February","March","April","May","June","July","August","September","October","November","December"
    32,42,22,42,52,62,32,17,0,0,0,0
    69,99,81,94,102,55,88,52,0,0,0,0
    22,44,41,77,79,22,52,31,0,0,0,0    

    It kind of works on the first GetMonths(), but the chart reads "January","February","March","April","May","June","July","August","September","October","November","December" across the bottom. the second GetMonths() doesnt work at all. 

    The goal is simple (in my mind), but I can't just get it to work. I need to read a line, put it in an array, and feed it back to the chart. I can't figure out why hard coding the values works and the first GetMonths() returns the exact text from the file (with all the "" around the value)

    Here's a sample of what works when hard coded, but this can't be the solution. Each customer has different values and the text file will hold those values. 

    [WebMethod]
    public string[] GetSvcs()
    {
       var results = new[]
       {
          "Antibiotics", "Antifungal", "Cardiac", "Cath Care", "Hydration", "Injection", "Steriod", "TPN", "Other"
       };
       return results;
    }

    Monday, August 27, 2018 11:14 PM

Answers

  • User-1003897979 posted

    Get the first line of the file and split that line into its components.  One way to do it is like this

    return File.ReadLines(filename)
               .First()
               .Split(",\"".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

    The Split method breaks the string on either a comma or double quote character and discards any empty entries

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 27, 2018 11:56 PM

All replies

  • User-1003897979 posted

    I don't understand your question.  You seem to be saying that if you feed an array of strings that look like treatments then you get the answer that you expect but that if you feed a file of months and numbers then you do not ???  Not really surprising.

    Anyway ...

    File.ReadLines returns an enumerable where each element will be a line of the file

    Skip(0) says don't skip any lines which is the same as doing nothing.

    Take(1) says I only want the first line of the file.  So you will return the first line of the file.  The first line of the file is the name of months, the method is called GetMonths and it returns the names of months, it seems to do what you want.

    What are you expecting to happen?

    Monday, August 27, 2018 11:33 PM
  • User-973788521 posted

    the treatments was merely an example of what works, it has nothing to do with Months, and can't be the solution. I can't tell C# that the months are "X", "Y", "Z". I have to let the text file tell C# what the months are. 

    What is happening is the chart is reading the first line in the text file as 1 continuous value. 

    "January","February","March","April","May","June","July","August","September","October","November","December"

    so instead of the <g class="gr_ gr_609 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="609" data-gr-id="609">X axis</g> getting12 values (for 12 months), it has only 1 as the entire string. I need each month to be it's own (<g class="gr_ gr_1216 gr-alert gr_tiny gr_spell gr_inline_cards gr_run_anim ContextualSpelling multiReplace" id="1216" data-gr-id="1216">i</g> guess) index in the array (<g class="gr_ gr_1125 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="1125" data-gr-id="1125">jan</g> as 0, <g class="gr_ gr_1182 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="1182" data-gr-id="1182">feb</g> as 1, march <g class="gr_ gr_1245 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del" id="1245" data-gr-id="1245">as</g> 2, etc.), so when the array returns values to the chart, the chart says, "hey look, I got 12 values, so let's build 12 points across the axis". 

    Monday, August 27, 2018 11:44 PM
  • User-1003897979 posted

    Get the first line of the file and split that line into its components.  One way to do it is like this

    return File.ReadLines(filename)
               .First()
               .Split(",\"".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

    The Split method breaks the string on either a comma or double quote character and discards any empty entries

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 27, 2018 11:56 PM
  • User-973788521 posted

    Exactly what I needed. Thank you

    Tuesday, August 28, 2018 12:14 AM