C# - Using Memory Streams with XML [modified]
-
I have come across an interesting problem. If I have a DataSet which I've read from an XML file. Then I call WriteXML to my memory stream. If I try read it back into a dataset I get the following error "Root Element is missing" here is some example code.
DataSet ds = new DataSet(); ds.ReadXmlSchema(@"C:\Foo\FooSchema.xsd"); ds.ReadXML(@"C:\Foo\Foo.xml"); MemoryStream ms = new MemoryStream(); ds.WriteXml(ms); ds.ReadXml(ms); //Error occurs.
Does anyone know why? or how I can use a DataSet to read directly from a MemoryStream?modified on Tuesday, October 20, 2009 9:58 AM
-
I have come across an interesting problem. If I have a DataSet which I've read from an XML file. Then I call WriteXML to my memory stream. If I try read it back into a dataset I get the following error "Root Element is missing" here is some example code.
DataSet ds = new DataSet(); ds.ReadXmlSchema(@"C:\Foo\FooSchema.xsd"); ds.ReadXML(@"C:\Foo\Foo.xml"); MemoryStream ms = new MemoryStream(); ds.WriteXml(ms); ds.ReadXml(ms); //Error occurs.
Does anyone know why? or how I can use a DataSet to read directly from a MemoryStream?modified on Tuesday, October 20, 2009 9:58 AM
The current position pointer of the memory stream is left at the end when you do the
ds.WriteXml
. You have to do something (apologies, no access to IDE right now) likems.CurrentPosition = 0;
before the theReadXml
to move it back to the start.CCC solved so far: 2 (including a Hard One!)
-
I have come across an interesting problem. If I have a DataSet which I've read from an XML file. Then I call WriteXML to my memory stream. If I try read it back into a dataset I get the following error "Root Element is missing" here is some example code.
DataSet ds = new DataSet(); ds.ReadXmlSchema(@"C:\Foo\FooSchema.xsd"); ds.ReadXML(@"C:\Foo\Foo.xml"); MemoryStream ms = new MemoryStream(); ds.WriteXml(ms); ds.ReadXml(ms); //Error occurs.
Does anyone know why? or how I can use a DataSet to read directly from a MemoryStream?modified on Tuesday, October 20, 2009 9:58 AM
-
In your example you try to read after the written data (MemoryStream position != 0). You have to reset the read position of the memory stream.
MemoryStream ms = new MemoryStream();
ds.WriteXml(ms);
ms.Seek(0, SeekOrigin.Begin);
ds.ReadXml(ms); -
I have come across an interesting problem. If I have a DataSet which I've read from an XML file. Then I call WriteXML to my memory stream. If I try read it back into a dataset I get the following error "Root Element is missing" here is some example code.
DataSet ds = new DataSet(); ds.ReadXmlSchema(@"C:\Foo\FooSchema.xsd"); ds.ReadXML(@"C:\Foo\Foo.xml"); MemoryStream ms = new MemoryStream(); ds.WriteXml(ms); ds.ReadXml(ms); //Error occurs.
Does anyone know why? or how I can use a DataSet to read directly from a MemoryStream?modified on Tuesday, October 20, 2009 9:58 AM
Keefb is coirrect, you have to rewind the stream, using:
if (ms.CanSeek)
{
ms.Seeek(0,SeekOrigin.Begin);
}But be aware it may not work! You may well need ms.FlushFinalBlock() to ensure that last bit of data is written.
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones
-
I have come across an interesting problem. If I have a DataSet which I've read from an XML file. Then I call WriteXML to my memory stream. If I try read it back into a dataset I get the following error "Root Element is missing" here is some example code.
DataSet ds = new DataSet(); ds.ReadXmlSchema(@"C:\Foo\FooSchema.xsd"); ds.ReadXML(@"C:\Foo\Foo.xml"); MemoryStream ms = new MemoryStream(); ds.WriteXml(ms); ds.ReadXml(ms); //Error occurs.
Does anyone know why? or how I can use a DataSet to read directly from a MemoryStream?modified on Tuesday, October 20, 2009 9:58 AM
If you look at the value of
ms.Position
after you have wrote in it, you'll see that has the same value ofms.Length
. That is because you have just written into the stream and the stream cursor is located at the end. Due to that, if you try to read the stream content, the reader doesn't found anything to read, giving the "Root Element is missing" exception. Callms.Seek(0, SeekOrigin.Begin)
(or setms.Position = 0
) to move the cursor back to the beginning of the stream after writing and before reading. -
I have come across an interesting problem. If I have a DataSet which I've read from an XML file. Then I call WriteXML to my memory stream. If I try read it back into a dataset I get the following error "Root Element is missing" here is some example code.
DataSet ds = new DataSet(); ds.ReadXmlSchema(@"C:\Foo\FooSchema.xsd"); ds.ReadXML(@"C:\Foo\Foo.xml"); MemoryStream ms = new MemoryStream(); ds.WriteXml(ms); ds.ReadXml(ms); //Error occurs.
Does anyone know why? or how I can use a DataSet to read directly from a MemoryStream?modified on Tuesday, October 20, 2009 9:58 AM
if all else fails, maybe try and set
ms.Position=0;
:laugh:Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? 59.24% waren verstandig genoeg om NEEN te stemmen; bye bye viaduct.