I had a Dataset with some data in it. When I tried to write this DataSet into a file, everything was OK. But When I tried to write it into a MemoryStream, the XML file declaration was lost. The code looks like:
DataSet dSet = new DataSet(); //load schema, fill data in dSet.WriteXML('testFile.xml'); MemoryStream stream = new MemoryStream(); dSet.WriteXML(stream); stream.Seek(0,SeekOrigin.Begin);
When I opened file testFile.xml, I got:
<?xml version='1.0' standalone='yes'?> //balabala
But When I open the stream with StreamReader, I only got:
//balabala
Somebody said I can insert XML file declaration in my stream manually. It works but seems so ugly. Do you know why it dropped the first line and any more simple solution?
It wasn’t dropped. Simply not included. Though it is highly recommend the xml declaration is not a required element of the xml specification.
http://msdn.microsoft.com/en-us/library/ms256048(VS.85).aspx
You can use XmlWriter.WriteStartDocument to include the xml declaration in the stream like so: