DataSet to a Tab delimited file

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi
I have a DataSet file (xml) which I need to convert it into a tab delimited
file. I need to write a C# console application for doing the same. Can anyone
help me out with the code to do it? I'd appreciate any kind of help.

Thanks
 
Assuming you only have 1 table in the dataset, it should be as simple as:

int numColumns = ds.Tables[0].Columns.Count;
for(int row=0; row<ds.Tables[0].Rows.Count; rows++)
{
for(int col=0; col < numColumns - 1; col++)
{
writer.Write(ds.Tables[0].Rows[row][col].ToString() + '\t');
}
writer.WriteLine(ds.Tables[0].Rows[row][numColumns - 1].ToString());
}

Where:
ds = DataSet
writer = a type derived from TextWriter (example: StreamWriter is
probably your best bet)


Note - I make no claim that this is the fastest, most efficient method,
but it should do the job.

If you have more than one table in your dataset, you have to decide how
you want to handle the other tables - do they each get their own file?


Joshua Flanagan
http://flimflan.com/blog
 
Joshua
Thanks for your reply. But my question is I basically have the dataset
itself. So when I say
int numColumns = ds.Tables[0].Columns.Count;
i guess it would give me an error about the ds.
The thing is am not getting the data from the database and filling the
dataset. Instead I have a response xml file which has the dataset itself. In
such case how do i populate the dataset ds?

Thanks
Deepa

Joshua Flanagan said:
Assuming you only have 1 table in the dataset, it should be as simple as:

int numColumns = ds.Tables[0].Columns.Count;
for(int row=0; row<ds.Tables[0].Rows.Count; rows++)
{
for(int col=0; col < numColumns - 1; col++)
{
writer.Write(ds.Tables[0].Rows[row][col].ToString() + '\t');
}
writer.WriteLine(ds.Tables[0].Rows[row][numColumns - 1].ToString());
}

Where:
ds = DataSet
writer = a type derived from TextWriter (example: StreamWriter is
probably your best bet)


Note - I make no claim that this is the fastest, most efficient method,
but it should do the job.

If you have more than one table in your dataset, you have to decide how
you want to handle the other tables - do they each get their own file?


Joshua Flanagan
http://flimflan.com/blog
Hi
I have a DataSet file (xml) which I need to convert it into a tab delimited
file. I need to write a C# console application for doing the same. Can anyone
help me out with the code to do it? I'd appreciate any kind of help.

Thanks
 
Now you have me confused.

I guess you are saying you do NOT have an instance of a
System.Data.DataSet class.
You only have an XML file, which contains a SET of DATA - which has
nothing to do with System.Data.DataSet. Is that correct?

If that is the case, you could try creating an XSLT to transform the XML
input to tab-delimeted output. Look for some XSL transofmration tutorials.


How does an xml
Joshua
Thanks for your reply. But my question is I basically have the dataset
itself. So when I say
int numColumns = ds.Tables[0].Columns.Count;
i guess it would give me an error about the ds.
The thing is am not getting the data from the database and filling the
dataset. Instead I have a response xml file which has the dataset itself. In
such case how do i populate the dataset ds?

Thanks
Deepa

:

Assuming you only have 1 table in the dataset, it should be as simple as:

int numColumns = ds.Tables[0].Columns.Count;
for(int row=0; row<ds.Tables[0].Rows.Count; rows++)
{
for(int col=0; col < numColumns - 1; col++)
{
writer.Write(ds.Tables[0].Rows[row][col].ToString() + '\t');
}
writer.WriteLine(ds.Tables[0].Rows[row][numColumns - 1].ToString());
}

Where:
ds = DataSet
writer = a type derived from TextWriter (example: StreamWriter is
probably your best bet)


Note - I make no claim that this is the fastest, most efficient method,
but it should do the job.

If you have more than one table in your dataset, you have to decide how
you want to handle the other tables - do they each get their own file?


Joshua Flanagan
http://flimflan.com/blog
Hi
I have a DataSet file (xml) which I need to convert it into a tab delimited
file. I need to write a C# console application for doing the same. Can anyone
help me out with the code to do it? I'd appreciate any kind of help.

Thanks
 
Back
Top