csvReared.Read method dosn't read the memory stream - c#

Please tell me tell me what I'm doing wrong
using (var mem = new MemoryStream())
{
var writer = new StreamWriter(mem);
writer.Write(csvText);
writer.Flush();
using (var reader = new StreamReader(mem))
{
mem.Seek(0, SeekOrigin.Begin);
using (var csvReader = new CsvReader(reader))
{
while (csvReader.Read())
{
// some unreach code
}
}
}
writer.Dispose();
}
The code inside while is unreached, and I can't understand why. csvText is a not empty string variable.
But the reader can't read it. I understand that most likely I made a stupid mistake, but I will be glad to resolve it. Thanks.

Related

Csvhelper how to create custom Headers with Localization?

I am trying to create a csv with Headers that are going to be use in Multiple languages. this is the current Code that i am using and works for English but not for Japanese as example.
What is the best way to do that? should i have to specify the Culture Info? i tried that but not good results.
Thanks.
private byte[] WriteCsvToMemory(IEnumerable records)
{
using (var memoryStream = new MemoryStream())
using (var streamWriter = new StreamWriter(memoryStream, Encoding.UTF8))
using (var csvWriter = new CsvWriter(streamWriter))
{
csvWriter.WriteField(GetResourceFileValue("ja-jp", "KEY"));
csvWriter.NextRecord();
csvWriter.Configuration.HasHeaderRecord = false;
csvWriter.WriteRecords(records);
streamWriter.Flush();
return memoryStream.ToArray();
}
}
i made a mistake on my code the solution is simple:
using (var streamWriter = new StreamWriter(memoryStream, **Encoding.UTF8** ))
Nothing wrong with the the library.

CsvHelper not writing data to memory stream

Having some problems with CsvHelper and writing to a memory stream. I've tried flushing the stream writer and setting positions and everything else tried. I figure I've narrowed it down to a really simple test case that obviously fails. What am I doing wrong here?
public OutputFile GetTestFile()
{
using (var ms = new MemoryStream())
using (var sr = new StreamWriter(ms))
using (var csv = new CsvWriter(sr))
{
csv.WriteField("test");
sr.Flush();
return new OutputFile
{
Data = ms.ToArray(),
Length = ms.Length,
DataType = "text/csv",
FileName = "test.csv"
};
}
}
[TestMethod]
public void TestWritingToMemoryStream()
{
var file = GetTestFile();
Assert.IsFalse(file.Data.Length == 0);
}
Editing the correct answer in for people googling as this corrected code actually passes my test. I have no idea why writing to a StringWriter then converting it to bytes solves all the crazy flushing issues, but it works now.
using (var sw = new StringWriter())
using (var csvWriter = new CsvWriter(sw, config))
{
csvWriter.WriteRecords(records);
return Encoding.UTF8.GetBytes(sw.ToString());
}
Since CSVHelper is meant to collect several fields per row/line, it does some buffering itself until you tell it the current record is done:
csv.WriteField("test");
csv.NextRecord();
sr.Flush();
Now, the memstream should have the data in it. However, unless there is more processing elsewhere, the result in your OutputFile is wrong: Data will be byte[] not "text/csv". It seems like StringWriter would produce something more appropriate:
string sBuff;
using (StringWriter sw = new StringWriter())
using (CsvWriter csv = new CsvWriter(sw))
{
csv.WriteRecord<SomeItem>(r);
sBuff = sw.ToString();
}
Console.WriteLine(sBuff);
"New Item ",Falcon,7

Using DataContractJsonSerializer to create a Non XML Json file

I want to use the DataContractJsonSerializer to serialize to file in JsonFormat.
The problem is that the WriteObjectmethod only has 3 options XmlWriter, XmlDictionaryWriter and Stream.
To get what I want I used the following code:
var js = new DataContractJsonSerializer(typeof(T), _knownTypes);
using (var ms = new MemoryStream())
{
js.WriteObject(ms, item);
ms.Position = 0;
using (var sr = new StreamReader(ms))
{
using (var writer = new StreamWriter(path, false))
{
string jsonData = sr.ReadToEnd();
writer.Write(jsonData);
}
}
}
Is this the only way or have I missed something?
Assuming you're just trying to write the text to a file, it's not clear why you're writing it to a MemoryStream first. You can just use:
var js = new DataContractJsonSerializer(typeof(T), _knownTypes);
using (var stream = File.Create(path))
{
js.WriteObject(stream, item);
}
That's rather simpler, and should do what you want...
I am actually quite terrified to claim to know something that Jon Skeet doesn't, but I have used code similar to the following which produces the Json text file and maintains proper indentation:
var js = new DataContractJsonSerializer(typeof(T), _knownTypes);
using (var stream = File.Create(path))
{
using (var writer = JsonReaderWriterFactory.CreateJsonWriter(stream, Encoding.UTF8, true, true, "\t"))
{
js.WriteObject(writer, item);
writer.Flush();
}
}
(as suggested here.)

Not disposing MemoryStream has side affects when disposing the StreamReader that uses it?

Given the following code snippet:
try
{
var myTxt = "";
var serializer = new DataContractSerializer(myObject.GetType());
var memoryStream = new MemoryStream()
serializer.WriteObject(memoryStream, myObject);
memoryStream.Position = 0;
using (var reader = new StreamReader(memoryStream))
{
myTxt = reader.ReadToEnd();
}
.
.
.
}
catch (IOException ioEx)
{
//log or whatever...
throw;
}
Typically I'd have a using statement around my memory stream,
but from my understanding disposing the StreamReader will close the Memory Stream.
So the question is, is there anything REALLY wrong with the above?
You could simplify your code as I don't see the point of writing, rewinding and reading:
var serializer = new DataContractSerializer(facets.GetType());
using (var stream = new MemoryStream())
{
serializer.WriteObject(stream, facets);
string xml = Encoding.UTF8.GetString(stream.ToArray());
}
I think its fine because MemoryStream doesn't hold unmanaged resources anyway, but if you wanted to be extra safe you could do this:
var memoryStream = new MemoryStream()
StreamReader reader = null;
try{
serializer.WriteObject(memoryStream, myObject);
memoryStream.Position = 0;
reader = new StreamReader(memoryStream)
//...
}
finally
{
if(reader != null)
reader.Dispose();
else
memoryStream.Dispose();
}
It is a good practice to use either
using (var stream = new MemoryStream(...))
using (var reader = new StreamReader(stream))
{
myTxt = reader.ReadToEnd();
}
Here both stream and reader will be disposed by runtime
or Close() manually both reader(writer) and stream
To test your scenario, I've added
memoryStream.Seek(0, SeekOrigin.Begin);
to the end and received an System.ObjectDisposedException, your code seems valid.
Of course vcsjones's comment is also valid.

In C#, why can't I access this stream?

I got this block of code from the NHibernate 3 Cookbook (an excellent book, btw), and while I couldn't find anyting in the errata about it. I'm getting the error "Cannot access a closed stream":
var settings = new XmlWriterSettings { Indent = true };
var serializer = new XmlSerializer(typeof(HbmMapping)); // todo: probably should abstract this out, at least optionally
using (var memStream = new MemoryStream(2048))
using (var xmlWriter = XmlWriter.Create(memStream, settings))
{
serializer.Serialize(xmlWriter, hbmMapping);
memStream.Flush();
memStream.Position = 0;
using (var sr = new StreamReader(memStream))
{
return sr.ReadToEnd();
}
}
The error is thrown on the sr.ReadToEnd() line.
Found a similar problem after all, Why disposing StreamReader makes a stream unreadable? . Basically, I removed the using that was around the reader and all is well.

Categories