在 C# 中,如何从字符串创建 TextReader 对象(不写入磁盘)
我正在使用 快速 CSV 阅读器 将一些粘贴的文本解析到网页中。 Fast CSV 阅读器需要一个 TextReader 对象,而我拥有的只是一个字符串。将字符串即时转换为 TextReader 对象的最佳方法是什么?
谢谢!
更新- 示例代码 - 在原始示例中,新的 StreamReader 正在查找名为“data.csv”的文件。我希望通过 TextBox_StartData.Text 提供它。
使用下面的代码无法编译。
TextReader sr = new StringReader(TextBox_StartData.Text);
using (CsvReader csv = new CsvReader(new StreamReader(sr), true))
{
DetailsView1.DataSource = csv;
DetailsView1.DataBind();
}
new StreamReader(sr)
告诉我它有一些无效的参数。有什么想法吗?
作为替代方法,我尝试了以下方法:
TextReader sr = new StreamReader(TextBox_StartData.Text);
using (CsvReader csv = new CsvReader(sr, true))
{
DetailsView1.DataSource = csv;
DetailsView1.DataBind();
}
但我收到 路径中的非法字符错误。
这是 TextBox_StartData.Text 中的字符串示例:
Fname\tLname\tEmail\nClaude\tCuriel\[email protected]\nAntoinette\tCalixte\[email protected]\nCathey\tPeden\[email protected]\n
如果这是正确的方法,有什么想法吗?再次感谢您的帮助!
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\[email protected]\nAntoinette\tCalixte\[email protected]\nCathey\tPeden\[email protected]\n
Any ideas if this the right approach? Thanks again for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
使用 System.IO.StringReader :
Use System.IO.StringReader :
使用继承
TextReader
的StringReader
类。Use the
StringReader
class, which inheritsTextReader
.StringReader
是一个TextReader
(StreamReader
也是,但用于从流中读取)。因此,以您的第一个示例为例,仅使用它来构造CsvReader
而不是尝试首先从中构造StreamReader
给出:StringReader
is aTextReader
(StreamReader
is too, but for reading from streams). So taking your first example and just using it to construct theCsvReader
rather than trying to construct aStreamReader
from it first gives:您需要一个 StringReader
You want a StringReader
只需使用 StringReader 类即可。它继承自TextReader。
Simply use the StringReader class. It inherits from TextReader.
如果您查看 TextReader 的文档 ,你会看到两个继承类。其中之一是
StringReader
,这似乎正是你想要的。If you look at the documentation for
TextReader
, you will see two inheriting classes. And one of them isStringReader
, which seems to do exactly what you want.