Application of generics in reading CVS data

Source: Internet
Author: User

CVS is a lightweight database, and text is separated by commas. In combination with generics, the method for reading this file is as follows:

 

Code

  Public  List  <  Dictionary  <  String  ,  String  >  Getdata (  String Filepath,  Out  List  <  String  >  Column)
{
Column = New List < String > ();
List < Dictionary < String , String > Data = New List < Dictionary < String , String > ();
String Strline = "" ;
String [] Strarray;
Streamreader SR = New Streamreader (filepath );
Strline = Sr. Readline ();
If (Strline = Null | Strline = "" )
{
Return New List < Dictionary < String , String > ();
}
Strarray = Strline. Split ( ' , ' );
Foreach ( String Item In Strarray)
{
Column. Add (item );
}
Strline = Sr. Readline ();
While (Strline ! = Null )
{
Strarray = Strline. Split ( ' , ' );
Dictionary < String , String > Dict = New Dictionary < String , String > ();
For ( Int I = 0 ; I < Column. Count; I ++ )
{
Dict. Add (column [I], strarray [I]);
}
Data. Add (dict );
Strline = Sr. Readline ();
}
Sr. Close ();
Return Data;
}

 

The call method is as follows:

List <string> column;
List <dictionary <string, string> date = getdata (this. textbox1.text. Trim (), Out column );
String STR = "";
Foreach (Dictionary <string, string> item in date)
{
Foreach (string Item1 in column)
{
STR + = item [Item1] + "";
}
STR + = environment. newline;

}
This. textbox2.text = STR;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.