The Problem of WriteXml

A

ad

I test the XmlWrite/XmlRead from MemoryStrem, my codes is:

DataSet ds = Will.DM.ExecuteDataset("Select * from Customer);
MemoryStream msOri = new MemoryStream();
ds.WriteXml(msOri);

DataSet ds2 = new DataSet();
ds2.ReadXml(msOri);

It compiled ok, but when it run fail at the last line: ds.WriteXml(msOri);
Ther error message is
System.Xml.XmlException: Root element is missing.

The msOri is write form ds, why it can't read to ds2?
 
N

Nicholas Paldino [.NET/C# MVP]

The reason is because the pointer in the stream is at the end of the
stream.

You need to reset the position to the beginning of the stream. Before
the call to ReadXml, do this:

// Set the position in the stream to the beginning.
msOri.Position = 0;

Hope this helps.
 
V

VJ

ds.WriteXml(msOri, XmlWriteMode.WriteSchema);

Schema.. has to be saved.. if you want to read back to a DataSet...
 
A

ad

Thanks, You give me a great help!


Nicholas Paldino said:
The reason is because the pointer in the stream is at the end of the
stream.

You need to reset the position to the beginning of the stream. Before
the call to ReadXml, do this:

// Set the position in the stream to the beginning.
msOri.Position = 0;

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

ad said:
I test the XmlWrite/XmlRead from MemoryStrem, my codes is:

DataSet ds = Will.DM.ExecuteDataset("Select * from Customer);
MemoryStream msOri = new MemoryStream();
ds.WriteXml(msOri);

DataSet ds2 = new DataSet();
ds2.ReadXml(msOri);

It compiled ok, but when it run fail at the last line:
ds.WriteXml(msOri);
Ther error message is
System.Xml.XmlException: Root element is missing.

The msOri is write form ds, why it can't read to ds2?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top