How to read Json Data from online file?

C# split-- read from a file and split the file for specific data

  • I need to read from a file and split the file for specific data and send the results to an index in C# Thank you

  • Answer:

    Hi, amy123456-ga: Here is a revised version of the code which properly extracts the data items from the sample file. Some comments on how it works follow the code. /* begin textextract.cs */ using System; using System.IO; using System.Text; using System.Text.RegularExpressions; namespace textextract { /// <summary> /// Main Class TxtXtrct for text extract console app. /// </summary> class TxtXtrct { public static void Main(string[] args) { if (args.Length == 0) { Console.WriteLine("You must specify a filename."); } else { TxtXtrct.ListItems(args[0]); } return; } /// <summary> /// Method for extracting text items from file /// </summary> /// <param name="filename">root/path/filename.ext</param> static void ListItems(string filename) { if (!File.Exists(filename)) { Console.WriteLine( "Error: Specified file {0} does not exist.", filename); return; } StreamReader sr = File.OpenText(filename); while(sr.Peek() > -1) { if (Regex.IsMatch(sr.ReadLine(), "Order Session")) break; } if (sr.Peek() == -1) { Console.WriteLine( "Error: Order Session header was not found."); sr.Close(); return; } while (sr.Peek() > -1) { string extractLine = sr.ReadLine(); if (Regex.IsMatch(extractLine, ",")) { // found the line with the items of interest // the last name is the trimmed part in front of the comma string lastname = extractLine.Substring( 0, extractLine.IndexOf(",")).Trim(); Console.WriteLine("Last name: " + lastname); // the first name is trimmed next part up to triple space string extract1 = extractLine.Substring( extractLine.IndexOf(",") + 1); string firstname = extract1.Substring( 0, extract1.IndexOf(" ")).Trim(); Console.WriteLine("First name: " + firstname); // the location is next after first name string extract2 = extract1.Substring( extract1.IndexOf(firstname + " ") + firstname.Length ).Trim(); string location = extract2.Substring( 0, extract2.IndexOf(" ")).Trim(); Console.WriteLine("Location: " + location); // the last item room number is next, up to a dash string extract3 = extract2.Substring( extract2.IndexOf(location + " ") + location.Length ).Trim(); string roomnumber = extract3.Substring( 0, extract3.IndexOf("-")).Trim(); Console.WriteLine("Room number: " + roomnumber); break; } } sr.Close(); return; } } } /* end of textextract.cs */ This program is intended to illustrate some useful techniques for extract specific data items from a text file. First, of course, we test for the existence of a file (given its name) and open that file with a "StreamReader" object. Then we read lines from the StreamReader and manipulate the resulting strings in various ways. The "regular expression" class Regex allows us to match a variety of patterns within strings, but the use made here is minimal. We look first for a line that contains "Order Session", and subsequently for a line that has a comma. Once we have that line, we use the Substring and IndexOf methods of the string class to extract the data items we need. This code: lastname = extractLine.Substring(0, extractLine.IndexOf(",")).Trim(); illustrates the ideas. We find where the comma is in the string extractLine with the expression: extractLine.IndexOf(",") The characters in a string are identified by zero-based indexing. Thus when we ask for the substring beginning at position 0 (the first character) having length extractLine.IndexOf(","), this gives everything in front of (but not including) the comma character. The final Trim() method removes any whitespace from both ends of the result. In the next step we use Substring with a single argument: extract1 = extractLine.Substring(extractLine.IndexOf(",") + 1); which starts at one position beyond the comma and takes the rest of the string. From here we would continue to extract the first name and other items until we are finished. If you have any questions about the details of the code, please post a Request for Clarification. The structure of the code is linear, in the sense of proceding forward through the file in one pass. While this seems adequate for the immediate task, parsing text often requires a more complicated arrangements. As your project progresses you might need to revisit this code and make a more elaborate search logic. Especially if the code proves unreliable, we might need to wrap the search in a try/catch exception handler. Further testing will hopefully help you decide if this is the case. regards, mathtalk-ga

amy123456-ga at Google Answers Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.