How to implement string (xml) in memystream for a file in C #?

I have a string (xml), and I need to temporarily store it as a memory stream, and then save it as a file at the end.

I know that we can directly store xml in a file using a text editor, but that is not what I want. I want the string to be converted to a memory stream and then written to the stream.

how can i implement this? Code sharing will be very helpful.

+3
source share
2 answers

If I get you, do you want to open the memory stream in 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);
+9
source

.NET 4, - Stream.CopyTo, MemoryStream FileStream .

, . :

byte[] buffer = new byte[4096];
int read;
while ((read = source.Read(buffer, 0, buffer.Length)) != 0) 
{
    destination.Write(buffer, 0, read);
}
+3

Source: https://habr.com/ru/post/1734580/


All Articles