473,386 Members | 1,819 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Array to Xml to DataSet

I have an array of objects (my own custom class). I'm trying to convert
that array to a dataset. So I serialize the array to XML, then try to load
the XML to a dataset.

MemoryStream mem = new MemoryStream();
XmlTextWriter w = new XmlTextWriter(mem,System.Text.Encoding.UTF8);
ser.Serialize(w,books);
XmlTextReader r = new XmlTextReader(mem);
DataSet dsBooks = new DataSet();
dsBooks.ReadXml(r); <=== error here

However, the last line generates System.Xml.XmlException. Additional info
is "System Error". Does anyone know the best way to do this? I prefer not
to write to disk. Thanks for any advice!

--Marty
Nov 12 '05 #1
2 8630
Did you try to "rewind" the MemoryStream?

After you serialized the objects to XML, the stream is positioned at
right behind the last character.

In order to read the contents of the stream into the DataSet you need to
position the stream at the beginning of the content:
MemoryStream mem = new MemoryStream();
XmlTextWriter w = new XmlTextWriter(mem,System.Text.Encoding.UTF8);
ser.Serialize(w,books);
mem.Position = 0;

Should do the trick.

HTH,
Christoph Schittko
MS MVP XML
http://weblogs.asp.net/cschittko

-----Original Message-----
From: Marty McDonald [mailto:mc******@wsdot.wa.gov]
Posted At: Tuesday, November 09, 2004 7:02 PM
Posted To: microsoft.public.dotnet.xml
Conversation: Array to Xml to DataSet
Subject: Array to Xml to DataSet

I have an array of objects (my own custom class). I'm trying to convert that array to a dataset. So I serialize the array to XML, then try to
load
the XML to a dataset.

MemoryStream mem = new MemoryStream();
XmlTextWriter w = new XmlTextWriter(mem,System.Text.Encoding.UTF8);
ser.Serialize(w,books);
XmlTextReader r = new XmlTextReader(mem);
DataSet dsBooks = new DataSet();
dsBooks.ReadXml(r); <=== error here

However, the last line generates System.Xml.XmlException. Additional info is "System Error". Does anyone know the best way to do this? I prefer not
to write to disk. Thanks for any advice!

--Marty

Nov 12 '05 #2
I was not resetting the memory pointer, I did not know I needed to do that.
I used your suggestion, and now the code works perfectly.

MemoryStream mem = new MemoryStream();
XmlTextWriter w = new XmlTextWriter(mem,System.Text.Encoding.UTF8);
ser.Serialize(w,books);
mem.Position = 0; <=== I added this
XmlTextReader r = new XmlTextReader(mem);
DataSet dsBooks = new DataSet();
dsBooks.ReadXml(r);

Thanks very much Christoph!
--Marty

"Christoph Schittko [MVP]" <IN**********@austin.rr.com> wrote in message
news:O0**************@TK2MSFTNGP12.phx.gbl...
Did you try to "rewind" the MemoryStream?

After you serialized the objects to XML, the stream is positioned at
right behind the last character.

In order to read the contents of the stream into the DataSet you need to
position the stream at the beginning of the content:
MemoryStream mem = new MemoryStream();
XmlTextWriter w = new XmlTextWriter(mem,System.Text.Encoding.UTF8);
ser.Serialize(w,books);


mem.Position = 0;

Should do the trick.

HTH,
Christoph Schittko
MS MVP XML
http://weblogs.asp.net/cschittko

-----Original Message-----
From: Marty McDonald [mailto:mc******@wsdot.wa.gov]
Posted At: Tuesday, November 09, 2004 7:02 PM
Posted To: microsoft.public.dotnet.xml
Conversation: Array to Xml to DataSet
Subject: Array to Xml to DataSet

I have an array of objects (my own custom class). I'm trying to

convert
that array to a dataset. So I serialize the array to XML, then try to
load
the XML to a dataset.

MemoryStream mem = new MemoryStream();
XmlTextWriter w = new XmlTextWriter(mem,System.Text.Encoding.UTF8);
ser.Serialize(w,books);
XmlTextReader r = new XmlTextReader(mem);
DataSet dsBooks = new DataSet();
dsBooks.ReadXml(r); <=== error here

However, the last line generates System.Xml.XmlException. Additional

info
is "System Error". Does anyone know the best way to do this? I

prefer
not
to write to disk. Thanks for any advice!

--Marty


Nov 12 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: EMiller | last post by:
Hello, I am encountering a development challenge here that seems to be stumping me. I am developing a C#/.NET application using an MSDE database. There is a particular field in a table that I...
6
by: Carl | last post by:
Hi, I want to pass an array of object of different type to a function and use it in the function. I am not able to have it working. Function that create the array... public virtual DataSet...
1
by: STeve | last post by:
Hi guys, I was wondering how I would go about creating a string array from a single column from a dataset. What I was thinking was iterate through each row in the dataset, finding out the size...
4
by: ALI-R | last post by:
Is it a possibel to assign an array to a DataColumn then bind it to a Combo box?? Is the follwoing code right? public static DataSet InsertParamsToDS(ReportParameter arrParams) { DataSet...
6
by: Michael Rodriguez | last post by:
What is the proper way to declare and initialize an array of datasets? Would it go like this: private DataSet myDataSets; DataSet ds1 = new DataSet(); // initialize the dataset DataSet...
7
by: Jed | last post by:
I have a web service method that returns an array of custom objects marked serializeable with fully described public properties. When I bind the results to a DataGrid I can access the properties...
5
by: Geoff Jones | last post by:
Hi Suppose we have an array of DataRows e.g. Dim drMyRows() As DataRow = myTable.Rows(0).GetChildRows("PricesCompany") generated via a relationship. The question I have is this. Isn't...
1
by: elziko | last post by:
My intention is to store an array of singles inside a DataTable so that it can me peristed somehow, maybe XML file, maybe Access/SQL Server I don't know yet so I'm just saving it as an XML file for...
4
by: vaiism | last post by:
I am trying to write a string compare function using pointers and dynamic arrays, and am having trouble comparing the individual elements of the array contained with the struct. The code below...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.