Dear ladies and sirs.
I have an XElement
instance and I wish to write to a stream using XmlWriter
class. Why? Well, one of the configuration settings defines whether to use binary Xml or not. Based on this setting a suitable XmlWriter
instance is created - either by XmlWriter.Create(stream)
or XmlDictionaryWriter.CreateBinaryWriter(stream))
.
Anyway, I am trying the following code, but it leaves the stream empty:
using (var stream = new MemoryStream())
{
var xmlReader = new XDocument(xml).CreateReader();
xmlReader.MoveToContent();
var xmlWriter = GetXmlWriter(stream);
xmlWriter.WriteNode(xmlReader, true);
return stream.ToArray();
}
I have checked, xmlReader
is properly aligned after MoveToContent
at the root XML element.
I must be doing something wrong, but what?
Thanks.