473,396 Members | 1,894 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,396 software developers and data experts.

Problem reading an XML string without first writing to disk

I have a dataset that is returned from a sql server select that contains
metadata and a cached dataset saved in an xml column. I would like to
recover the dataset for use in generating a crystal pdf file. the
rptds.ReadXml method can accept either a string file indicating a filename
or a stream. the row["xmldata"].ToString() is the saved dataset. If I use
this directly it throws an error because it is data and not a filename. I
have a workaround seen below that first writes the xml to a temporary
diskfile and then reads it back in in the rptds.ReadXml statement. Although
this works, it is definitely kludgy... there must be a better way.

Any suggestions?

Sample Code:

public static void CreatePDF()
{
DataSet batchds = TestDAL.GetAllBatches();
foreach (DataRow row in batchds.Tables[0].Rows)
{
StreamWriter swtst = new StreamWriter(@"Test.xml", false,
Encoding.Default);
swtst.Write(row["xmldata"].ToString());
swtst.Close();
DataSet rptds = new DataSet();
rptds.ReadXml(@"Test.xml");
TestRpt rpt = new TestRpt ();
rpt.SetDataSource(rptds);
rpt.ExportToDisk(ExportFormatType.PortableDocForma t,
row["FileName"].ToString());
}
}
Apr 27 '06 #1
8 2354

Thirsty Traveler wrote:
I have a dataset that is returned from a sql server select that contains
metadata and a cached dataset saved in an xml column. I would like to
recover the dataset for use in generating a crystal pdf file. the
rptds.ReadXml method can accept either a string file indicating a filename
or a stream. the row["xmldata"].ToString() is the saved dataset. If I use
this directly it throws an error because it is data and not a filename. I
have a workaround seen below that first writes the xml to a temporary
diskfile and then reads it back in in the rptds.ReadXml statement. Although
this works, it is definitely kludgy... there must be a better way.


You said the answer yourself: rptds.ReadXml wants a file or a stream.
If we are not to give it a file, we must give it a stream. What kind of
stream can we make from data that is in memory already? A MemoryStream.
How do we make a MemoryStream? From an array of byte. How do we make an
array of byte from a string? With Encoding.GetBytes.

The details are left as an exercise for the reader.

Incidentally, I would be interested to know if anyone can find a method
that (unlike this one) doesn't involve using twice as much memory as
the string takes. Can we derive from Stream and create our own
StringStream, perhaps?

--
Larry Lard
Replies to group please

Apr 27 '06 #2

Try this post:

http://groups.google.com/group/micro...ataSet1&rnum=1

"Thirsty Traveler" <nf*@nospam.com> wrote in message
news:OT**************@TK2MSFTNGP05.phx.gbl...
I have a dataset that is returned from a sql server select that contains
metadata and a cached dataset saved in an xml column. I would like to
recover the dataset for use in generating a crystal pdf file. the
rptds.ReadXml method can accept either a string file indicating a filename
or a stream. the row["xmldata"].ToString() is the saved dataset. If I use
this directly it throws an error because it is data and not a filename. I
have a workaround seen below that first writes the xml to a temporary
diskfile and then reads it back in in the rptds.ReadXml statement. Although this works, it is definitely kludgy... there must be a better way.

Any suggestions?

Sample Code:

public static void CreatePDF()
{
DataSet batchds = TestDAL.GetAllBatches();
foreach (DataRow row in batchds.Tables[0].Rows)
{
StreamWriter swtst = new StreamWriter(@"Test.xml", false,
Encoding.Default);
swtst.Write(row["xmldata"].ToString());
swtst.Close();
DataSet rptds = new DataSet();
rptds.ReadXml(@"Test.xml");
TestRpt rpt = new TestRpt ();
rpt.SetDataSource(rptds);
rpt.ExportToDisk(ExportFormatType.PortableDocForma t,
row["FileName"].ToString());
}
}

Apr 27 '06 #3
Thanks... I was playing with MemoryStream but unable to get it to work...
Encoding.GetBytes was my missing link.

"Larry Lard" <la*******@hotmail.com> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.com...

Thirsty Traveler wrote:
I have a dataset that is returned from a sql server select that contains
metadata and a cached dataset saved in an xml column. I would like to
recover the dataset for use in generating a crystal pdf file. the
rptds.ReadXml method can accept either a string file indicating a
filename
or a stream. the row["xmldata"].ToString() is the saved dataset. If I use
this directly it throws an error because it is data and not a filename. I
have a workaround seen below that first writes the xml to a temporary
diskfile and then reads it back in in the rptds.ReadXml statement.
Although
this works, it is definitely kludgy... there must be a better way.


You said the answer yourself: rptds.ReadXml wants a file or a stream.
If we are not to give it a file, we must give it a stream. What kind of
stream can we make from data that is in memory already? A MemoryStream.
How do we make a MemoryStream? From an array of byte. How do we make an
array of byte from a string? With Encoding.GetBytes.

The details are left as an exercise for the reader.

Incidentally, I would be interested to know if anyone can find a method
that (unlike this one) doesn't involve using twice as much memory as
the string takes. Can we derive from Stream and create our own
StringStream, perhaps?

--
Larry Lard
Replies to group please

Apr 27 '06 #4
Larry Lard <la*******@hotmail.com> wrote:
You said the answer yourself: rptds.ReadXml wants a file or a stream.


That's not strictly true though. It wants a file, a stream, a
TextReader or an XmlReader. TextReader is what we want here -
StringReader, specifically.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 27 '06 #5
The following code works without having to write/read from disk:

public static void CreatePDF()
{
DataSet batchds = TestDAL.GetAllBatches();

foreach (DataRow row in batchds.Tables[0].Rows)
{
DataSet rptds = new DataSet();
byte[] b = Encoding.UTF8.GetBytes(row["Data"].ToString());
MemoryStream ms = new MemoryStream(b);

rptds.ReadXml(ms);

TestRpt rpt = new TestRpt ();
rpt.SetDataSource(rptds);

rpt.ExportToDisk(ExportFormatType.PortableDocForma t,
row["FileName"].ToString());
}
}

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Larry Lard <la*******@hotmail.com> wrote:
You said the answer yourself: rptds.ReadXml wants a file or a stream.


That's not strictly true though. It wants a file, a stream, a
TextReader or an XmlReader. TextReader is what we want here -
StringReader, specifically.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Apr 27 '06 #6
Thirsty Traveler <nf*@nospam.com> wrote:
The following code works without having to write/read from disk:


Yes, but it does create a copy of the data. Why not use create a
StringReader, as I suggested?

StringReader reader = new StringReader(row["Data"].ToString());
rptds.ReadXml(reader);

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 27 '06 #7

Jon wrote:
Larry Lard <la*******@hotmail.com> wrote:
You said the answer yourself: rptds.ReadXml wants a file or a stream.


That's not strictly true though. It wants a file, a stream, a
TextReader or an XmlReader. TextReader is what we want here -
StringReader, specifically.


That's what I get for trusting the info on the support call, I suppose
:)

--
Larry Lard
Replies to group please

Apr 27 '06 #8
Awesome... this final touch rendered the solution simple and elegant.

Thanks.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Thirsty Traveler <nf*@nospam.com> wrote:
The following code works without having to write/read from disk:


Yes, but it does create a copy of the data. Why not use create a
StringReader, as I suggested?

StringReader reader = new StringReader(row["Data"].ToString());
rptds.ReadXml(reader);

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Apr 28 '06 #9

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

Similar topics

6
by: Rami A. Kishek | last post by:
Hi - this mysterious behavior with shelve is just about to kill me. I hope someone here can shed some light. First of all, I have this piece of code which uses shelve to save instances of some...
2
by: Senthoorkumaran Punniamoorthy | last post by:
I am printing these information. print string.lower(info_res) print string.lower(md5sum(f_md5)) print len(string.lower(info_res)) print len(string.lower(md5sum(f_md5))) print...
2
by: Jeevan | last post by:
Hi, I have an array of data (which I am getting from a socket connection). I am working on a program which acts on this data but the program is written to work on data from a file (not from an...
11
by: Abhishek | last post by:
I have a problem transfering files using sockets from pocket pc(.net compact c#) to desktop(not using .net just mfc and sockets 2 API). The socket communication is not a issue and I am able to...
16
by: Ali | last post by:
Hi I want to write (or read) to a stream, but the data is not byte array I converted the data to byte array manually, but it is very slow, (becuse the data is very large) Is another way for this...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
34
by: Simon Wigzell | last post by:
document...focus() will scroll the form to move the specified text field into view on everything I have tried it with except Safari on the MAC. The form doesn't move. Any work around? Thanks.
19
by: rmr531 | last post by:
First of all I am very new to c++ so please bear with me. I am trying to create a program that keeps an inventory of items. I am trying to use a struct to store a product name, purchase price,...
4
by: utab | last post by:
Dear all, I have to interface some C code in C++, but I had a problem with sscanf function, it has been some time I have not used C and I could not figure out my problem. Simple code is below, I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.