Today, when I browsed the forum, I saw another topic about reading TXT text files. Insus. NET also wants to implement it based on its own ideas and share it with you.
Text files are complex, and retrieving data is also part of some text lines. To get more accurate data, Insus. NET writes several steps. Each step uses a class. After all, the program we write is object-oriented. First, create a text file under the site:
12, 16: 45 CST-0800 20111 322 52412, 16: 45 CST-0800 20111 642 53712, 16: 45 CST-0800 20111 752 3677View Code
For example:
The data to be extracted should be highlighted. Next, write a SourceDataList class:
This class is used to process rough data, remove the leading and trailing spaces of each line of text lines, and remove the rows with several consecutive spaces. Only one is retained. The text lines are separated by spaces, as long as the text lines are segmented into two or three sections.
Further, write another class DataParse. According to the class name, you will know that this is the first time you get the data.
This class can obtain data that is basically finalized. The text to be transferred is divided into two sections (space is the separator), which are determined by two sections and three sections.
If it is a text line with two segments. The first element is ":". After the colon is removed, you must determine whether all the characters are uppercase letters. The second element starts and ends with a single quotation mark. As long as all these conditions are met, we need the file line.
For three lines of text, the first and second elements are integers, and the third element should be all uppercase letters. The text line that meets the condition is the text line to be obtained.
Next, we will write another category to combine the data obtained above, DataStructures:
The data processed by this class is close to the data rows we need. Each row of data has three fields, one of which is a string, and the second and third fields are integer.
The last class DataHelper:
Process text files, obtain data, and collect data in the List (Of DataStructures) collection.
OK. Now we can display the obtained data. On the site, create a webpage. aspx and write it to the Page_Load event:
When running the webpage, you can see the following results: