在C#中,如何从字符串创建一个TextReader对象(不写入磁盘) [英] In C#, how can I create a TextReader object from a string (without writing to disk)
问题描述
我使用快速CSV读取器将一些粘贴的文本解析为网页。快速CSV阅读器需要一个TextReader对象,我所有的都是一个字符串。
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?
谢谢!
Update-
示例代码 - 在原始示例中,一个新的StreamReader正在寻找一个名为data.csv的文件。我希望通过TextBox_StartData.Text提供它。
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();
}
新的StreamReader(sr)
告诉我它有一些无效的参数。任何想法?
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();
}
但在路径中有一个
以下是来自TextBox_StartData.Text的字符串示例:
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!
推荐答案
using(TextReader sr = new StringReader(yourstring))
{
DoSomethingWithATextReader(sr);
}
这篇关于在C#中,如何从字符串创建一个TextReader对象(不写入磁盘)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!