Ok, a couple of things. I know it's not exactly obvious, but to get
the functionality that I think you're looking for, you will also need
to set reader.Settings.UseTextQualifier to false. Now to verify that
in your test case, you have a couple of flaws that you need to fix
besides setting that setting. Currently, you're writing out the
contents of data1 to the writer, when you want to be writing out the
contents of reader[0], otherwise you're not actually testing the
reader. But you also need to make sure you move that call to Write up
to before your second call to ReadRecord, so that reader[0] still has
the value you're looking for. I didn't feel like importing the actual
testing framework namespace so I used the builtin Assert and I took
out a couple of Asserts that I didn't care about, but I think the code
below is a successful proof. Also, to anyone else that might be just
reading this thread at some point without paying too much attention,
make sure that you're noticing the backslash escaping of C# that's
being used when hardcoding the test values, as it's easily overlooked.
var data1 = "a\\b";
using (var reader = CsvReader.Parse(data1))
{
reader.Settings.EscapeMode = EscapeMode.Backslash;
reader.Settings.UseTextQualifier = false;
using (var stream = new MemoryStream())
{
using (var writer = new CsvWriter(stream, ',', Encoding.Default))
{
writer.Settings.EscapeMode = EscapeMode.Backslash;
Debug.Assert(reader.ReadRecord());
writer.Write(reader[0]);
Debug.Assert(!reader.ReadRecord());
writer.Flush();
var buffer = stream.ToArray();
var data2 = Encoding.Default.GetString(buffer, 0,
buffer.Length);
Debug.Assert(data2 == data1);
}
}
}
Bruce Dunwiddie