146

I'm using A Fast CSV Reader to parse some pasted text into a webpage. The Fast CSV reader requires a TextReader object, and all I have is a string. What's the best way to convert a string into a TextReader object on the fly?

Thanks!

Update- Sample code- In the original sample, a new StreamReader is looking for a file called "data.csv". I'm hoping to supply it via TextBox_StartData.Text.

Using this code below doesn't compile.

        TextReader sr = new StringReader(TextBox_StartData.Text);
        using (CsvReader csv = new CsvReader(new StreamReader(sr), true))
        {
            DetailsView1.DataSource = csv;
            DetailsView1.DataBind();
        }

The new StreamReader(sr) tells me it has some invalid arguments. Any ideas?

As an alternate approach, I've tried this:

        TextReader sr = new StreamReader(TextBox_StartData.Text);
        using (CsvReader csv = new CsvReader(sr, true))
        {
            DetailsView1.DataSource = csv;
            DetailsView1.DataBind();
        }

but I get an Illegal characters in path Error. Here's a sample of the string from TextBox_StartData.Text:

Fname\tLname\tEmail\nClaude\tCuriel\tClaude.Curiel@email.com\nAntoinette\tCalixte\tAntoinette.Calixte@email.com\nCathey\tPeden\tCathey.Peden@email.com\n

Any ideas if this the right approach? Thanks again for your help!

JumpingJezza
  • 5,498
  • 11
  • 67
  • 106
Hairgami_Master
  • 5,429
  • 10
  • 45
  • 66

6 Answers6

293

Use System.IO.StringReader :

using(TextReader sr = new StringReader(yourstring))
{
    DoSomethingWithATextReader(sr);
}
Steve B
  • 36,818
  • 21
  • 101
  • 174
10

Use the StringReader class, which inherits TextReader.

Steve B
  • 36,818
  • 21
  • 101
  • 174
Ilia G
  • 10,043
  • 2
  • 40
  • 59
6

StringReader is a TextReader (StreamReader is too, but for reading from streams). So taking your first example and just using it to construct the CsvReader rather than trying to construct a StreamReader from it first gives:

TextReader sr = new StringReader(TextBox_StartData.Text);
using(CsvReader csv = new CsvReader(sr, true))
{
  DetailsView1.DataSource = csv;
  DetailsView1.DataBind();
}
Jon Hanna
  • 110,372
  • 10
  • 146
  • 251
  • Thanks Jon... I think there's a bug with the Fast CSV Framework. I'm getting a result that looks like this: !http://screencast.com/t/5wZRrjDMO... – Hairgami_Master Oct 20 '11 at 16:17
  • My CSV is fname,lname,email john,doe,jd@email.com – Hairgami_Master Oct 20 '11 at 16:21
  • 1
    That (after I view-source to see that you are linking to http://screencast.com/t/5wZRrjDMO anyway) looks like you are producing a series of arrays of strings (one for each line), and trying to render them, which results in the text "System.String[]" repeated. This sounds to me like a reasonable result from a CSV parser, not handled well. Try outputting it to a grid-view and see what happens. – Jon Hanna Oct 20 '11 at 16:24
  • Thanks Jon- Actually, I am using a GridView, I've tried a couple of them, but I'm guessing the data is being returned properly, it's just a matter of choosing the right Data Display Control..?? – Hairgami_Master Oct 20 '11 at 16:27
  • 1
    I tend not to make heavy use of controls, so there may be something there I'm missing. The output seems to be a series of arrays of strings (one array for each row, one string for each cell), which makes sense. Not sure why it's not working beyond that I'm afraid :( – Jon Hanna Oct 20 '11 at 16:30
5

You want a StringReader

var val = "test string";
var textReader = new StringReader(val);
scottm
  • 27,829
  • 22
  • 107
  • 159
2

Simply use the StringReader class. It inherits from TextReader.

Ucodia
  • 7,410
  • 11
  • 47
  • 81
1

If you look at the documentation for TextReader, you will see two inheriting classes. And one of them is StringReader, which seems to do exactly what you want.

svick
  • 236,525
  • 50
  • 385
  • 514