XmlWriter writes XML data to a file, stream,, text reader, or string.
It is fast, noncached and forward only.
Example
Write/read XML to memory stream.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
MemoryStream stream = new MemoryStream();
XmlWriter writer = XmlWriter.Create(stream);
writer.WriteStartDocument();
writer.WriteStartElement("Root");
writer.WriteElementString("Child", "1");
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Flush();
writer.Close();
stream.Position = 0; // Resets the position to 0 so we can read.
XmlReader reader = XmlReader.Create(stream);
while (reader.Read())
{
//
}
reader.Close();
}
}
}