How to implement string(xml) to memorystream to file in C#?
I have a string(xml) and I need to 开发者_如何学Gostore it temporarily as memory stream and then store it as file at the end.
I know we can directly store xml in a file using textwriter but that is not what I want. I want the string to be converted to memory stream and then write into filestream.
how can I implement this? Sharing the code will be very helpful.
If I get you, you want to open memory stream on a char array (string) that represents XML?
string xml;
MemoryStream ms = new MemoryStream(Encoding.ASCII.GetBytes(xml));
ms.DuStuf();
fileStream.Write(ms.GetBuffer(), 0, xml.Length);
If you're using .NET 4, this is very easy - you can just use Stream.CopyTo to copy your MemoryStream into a FileStream directly.
If you're using an older version, you'll need to implement this yourself. This tends to look like:
byte[] buffer = new byte[4096];
int read;
while ((read = source.Read(buffer, 0, buffer.Length)) != 0)
{
destination.Write(buffer, 0, read);
}
精彩评论