在 C# 中,如何从字符串创建 TextReader 对象(不写入磁盘)

发布于 2024-12-11 01:46:59 字数 1605 浏览 6 评论 0原文

我正在使用 快速 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

国粹 2024-12-18 01:46:59

使用 System.IO.StringReader

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

Use System.IO.StringReader :

using(TextReader sr = new StringReader(yourstring))
{
    DoSomethingWithATextReader(sr);
}
别挽留 2024-12-18 01:46:59

使用继承 TextReaderStringReader 类。

Use the StringReader class, which inherits TextReader.

软糖 2024-12-18 01:46:59

StringReader 一个TextReaderStreamReader也是,但用于从流中读取)。因此,以您的第一个示例为例,仅使用它来构造 CsvReader 而不是尝试首先从中构造 StreamReader 给出:

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

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();
}
淡紫姑娘! 2024-12-18 01:46:59

您需要一个 StringReader

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

You want a StringReader

var val = "test string";
var textReader = new StringReader(val);
青衫负雪 2024-12-18 01:46:59

只需使用 StringReader 类即可。它继承自TextReader。

Simply use the StringReader class. It inherits from TextReader.

隐诗 2024-12-18 01:46:59

如果您查看 TextReader 的文档 ,你会看到两个继承类。其中之一是 StringReader,这似乎正是你想要的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文