How to parse CSV file

P

Peter Afonin

Hello,

I need to parse a string that returns the domain DNS records and to put this
data into a DataTable. I don't have much experience in parsing strings, so
I'm not aware of the efficient way how to do this. I've found some examples
of CSV parser classes, but not sure how to use them in my case.

Here is an example of the string:

RRs\nsubname: @;priority: 5;address: mail.domainname.net.;rectype:
MX;\nsubname: dns;priority: 1;address: ns1.domainhost.net.;rectype:
NS;\nsubname: dns;priority: 2;address: ns2.domainhost.net.;rectype:
NS;\nsubname: help;priority: 0;address: 67.22.36.4;rectype: A;\nsubname:
mail;priority: 0;address: domainname.net.;rectype: CNAME;\nsubname:
support;priority: 0;address: help.domainhost.net.;rectype: CNAME;\nsubname:
text;priority: 0;address: \"tex value\";rectype: TXT;

Each record is separated by "\n". The columns are:

subname
rectype
priority
address

I would appreciate very much if you could point me to the right direction
how to parse this string efficiently.

Thank you,

Peter
 
C

Cor Ligthert[MVP]

Peter,

Use OleDB for this about 1000.000 samples on Internet how to do this.

Cor
 
P

Peter Afonin

Thank you, Cor. I'll try to find them, had no luck so far, perhaps using
wrong search terms.

Peter
 
I

Ignacio Machin ( .NET/ C# MVP )

Hello,

I need to parse a string that returns the domain DNS records and to put this
data into a DataTable. I don't have much experience in parsing strings, so
I'm not aware of the efficient way how to do this. I've found some examples
of CSV parser classes, but not sure how to use them in my case.

Here is an example of the string:

RRs\nsubname: @;priority: 5;address: mail.domainname.net.;rectype:
MX;\nsubname: dns;priority: 1;address: ns1.domainhost.net.;rectype:
NS;\nsubname: dns;priority: 2;address: ns2.domainhost.net.;rectype:
NS;\nsubname: help;priority: 0;address: 67.22.36.4;rectype: A;\nsubname:
mail;priority: 0;address: domainname.net.;rectype: CNAME;\nsubname:
support;priority: 0;address: help.domainhost.net.;rectype: CNAME;\nsubname:
text;priority: 0;address: \"tex value\";rectype: TXT;

Each record is separated by "\n". The columns are:

subname
rectype
priority
address

I would appreciate very much if you could point me to the right direction
how to parse this string efficiently.

Thank you,

Peter

Hi,

Go to opennetcf.org and download the source code of the library they
offer, they provide a very straigforward provider
 
P

Peter Afonin

Thank you very much everyone, I'll try it.

Peter

message
Hello,

I need to parse a string that returns the domain DNS records and to put
this
data into a DataTable. I don't have much experience in parsing strings, so
I'm not aware of the efficient way how to do this. I've found some
examples
of CSV parser classes, but not sure how to use them in my case.

Here is an example of the string:

RRs\nsubname: @;priority: 5;address: mail.domainname.net.;rectype:
MX;\nsubname: dns;priority: 1;address: ns1.domainhost.net.;rectype:
NS;\nsubname: dns;priority: 2;address: ns2.domainhost.net.;rectype:
NS;\nsubname: help;priority: 0;address: 67.22.36.4;rectype: A;\nsubname:
mail;priority: 0;address: domainname.net.;rectype: CNAME;\nsubname:
support;priority: 0;address: help.domainhost.net.;rectype:
CNAME;\nsubname:
text;priority: 0;address: \"tex value\";rectype: TXT;

Each record is separated by "\n". The columns are:

subname
rectype
priority
address

I would appreciate very much if you could point me to the right direction
how to parse this string efficiently.

Thank you,

Peter

Hi,

Go to opennetcf.org and download the source code of the library they
offer, they provide a very straigforward provider
 
C

Cor Ligthert[MVP]

Peter,

I saw it, all those samples are in Visual Basic, basicly it is this, have a
look at the extened properties as you have problems.

I just have copied this somewhere from Internet I did not test it, but I see
at first sight no problems, it is quiet standard.

\\\
string path = @"C:\TEST\";
System.Data.OleDb.OleDbConnection conn = new
System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=" + path + ";Extended
Properties=\"text;HDR=No;IMEX=1;FMT=Delimited\"");
System.Data.OleDb.OleDbCommand cmd = new
System.Data.OleDb.OleDbCommand("SELECT * FROM sample.csv", conn);
cmd.Connection = conn;
System.Data.OleDb.OleDbDataAdapter adapter = new
System.Data.OleDb.OleDbDataAdapter(cmd);
System.Data.DataSet ds = new DataSet();
conn.Open();
adapter.Fill(ds);
conn.Close();

///

Cor
 
P

Peter Afonin

Thank you, Cor.

I've tried this and some other examples. Unfortunately, I'm not dealing
exactly with CSV file, I'm dealing with a string, although I can probably
create a CSV file out of it using Stream. I'm trying to do this, but I'm not
sure that I'm going the right way. Here is my string again:

RRs\nsubname: @;priority: 5;address: mail.russianalaska.ru.;rectype:
MX;\nsubname: dns;priority: 1;address: ns1.seattlehost.net.;rectype:
NS;\nsubname: dns;priority: 2;address: ns2.seattlehost.net.;rectype:
NS;\nsubname: help;priority: 0;address: 67.18.36.3;rectype: A;\nsubname:
mail;priority: 0;address: russianalaska.ru.;rectype: CNAME;\nsubname:
support;priority: 0;address: help.seattlehost.net.;rectype: CNAME;\nsubname:
text;priority: 0;address: \"tex value\";rectype: TXT;\nSRV\nservice:
_sip._udp;rectype: SRV;priority: 1;weight: 0;port: 5060;target:
sip.russianalaska.ru.;\n

Peter
 
M

Marc Gravell

Well, as you say: that certainly isn't CSV ;-p

The first thing to do would be to understand the rules... especially
regarding things like semicolons in escaped literals - but assuming
something simple, you could do this with standard string functions:

string data = "RRs\nsubname: @;priority: 5;address:
mail.russianalaska.ru.;rectype: MX;\nsubname: dns;priority: 1;address:
ns1.seattlehost.net.;rectype: NS;\nsubname: dns;priority: 2;address:
ns2.seattlehost.net.;rectype: NS;\nsubname: help;priority: 0;address:
67.18.36.3;rectype: A;\nsubname: mail;priority: 0;address:
russianalaska.ru.;rectype: CNAME;\nsubname: support;priority:
0;address: help.seattlehost.net.;rectype: CNAME;\nsubname:
text;priority: 0;address: \"tex value\";rectype: TXT;\nSRV\nservice:
_sip._udp;rectype: SRV;priority: 1;weight: 0;port: 5060;target:
sip.russianalaska.ru.;\n";
string[] lines = data.Split('\n');
for (int i = 1; i < lines.Length; i++)
{ // (skip first line)
string[] pairs = lines.Split(';');
foreach (string pair in pairs)
{
int index = pair.IndexOf(':');
if (index < 0) continue;
string key = pair.Substring(0, index).Trim(),
value = pair.Substring(index + 1).Trim();

Console.WriteLine("{0}: {1}={2}", i, key, value);
}
}
 
P

Peter Afonin

Thank you, Marc, I'll try. I thought it might be easier to try to convert it
to CSV, but I guess not...

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top