In C#, how can I create a TextReader object from a string (without writing to disk)

C#CsvCsvhelperTextreader

C# Problem Overview


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!

C# Solutions


Solution 1 - C#

Use System.IO.StringReader :

using(TextReader sr = new StringReader(yourstring))
{
    DoSomethingWithATextReader(sr);
}

Solution 2 - C#

Use the StringReader class, which inherits TextReader.

Solution 3 - C#

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();
}

Solution 4 - C#

You want a StringReader

var val = "test string";
var textReader = new StringReader(val);

Solution 5 - C#

Simply use the StringReader class. It inherits from TextReader.

Solution 6 - C#

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHairgami_MasterView Question on Stackoverflow
Solution 1 - C#Steve BView Answer on Stackoverflow
Solution 2 - C#Ilia GView Answer on Stackoverflow
Solution 3 - C#Jon HannaView Answer on Stackoverflow
Solution 4 - C#scottmView Answer on Stackoverflow
Solution 5 - C#UcodiaView Answer on Stackoverflow
Solution 6 - C#svickView Answer on Stackoverflow