473,770 Members | 5,977 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.GetAllB atches();
foreach (DataRow row in batchds.Tables[0].Rows)
{
StreamWriter swtst = new StreamWriter(@" Test.xml", false,
Encoding.Defaul t);
swtst.Write(row["xmldata"].ToString());
swtst.Close();
DataSet rptds = new DataSet();
rptds.ReadXml(@ "Test.xml") ;
TestRpt rpt = new TestRpt ();
rpt.SetDataSour ce(rptds);
rpt.ExportToDis k(ExportFormatT ype.PortableDoc Format,
row["FileName"].ToString());
}
}
Apr 27 '06 #1
8 2377

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.GetByt es.

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******** ******@TK2MSFTN GP05.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.GetAllB atches();
foreach (DataRow row in batchds.Tables[0].Rows)
{
StreamWriter swtst = new StreamWriter(@" Test.xml", false,
Encoding.Defaul t);
swtst.Write(row["xmldata"].ToString());
swtst.Close();
DataSet rptds = new DataSet();
rptds.ReadXml(@ "Test.xml") ;
TestRpt rpt = new TestRpt ();
rpt.SetDataSour ce(rptds);
rpt.ExportToDis k(ExportFormatT ype.PortableDoc Format,
row["FileName"].ToString());
}
}

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

"Larry Lard" <la*******@hotm ail.com> wrote in message
news:11******** **************@ g10g2000cwb.goo glegroups.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.GetByt es.

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*******@hotm ail.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.co m>
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.GetAllB atches();

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

rptds.ReadXml(m s);

TestRpt rpt = new TestRpt ();
rpt.SetDataSour ce(rptds);

rpt.ExportToDis k(ExportFormatT ype.PortableDoc Format,
row["FileName"].ToString());
}
}

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Larry Lard <la*******@hotm ail.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.co m>
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(ro w["Data"].ToString());
rptds.ReadXml(r eader);

--
Jon Skeet - <sk***@pobox.co m>
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*******@hotm ail.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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
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(ro w["Data"].ToString());
rptds.ReadXml(r eader);

--
Jon Skeet - <sk***@pobox.co m>
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
4755
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 class I define. It works perfectly on an old machine (PII-400) running Python 2.2.1 under RedHat Linux 8.0. When I try to run it under Python for windows ME on a P-4 1.4 GHz, however, it keeps crashing on reading from the shelved file the second...
2
2565
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 str(string.lower(md5sum(f_md5)) == string.lower(info_res)) and the output are
2
3070
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 array). I cannot change anything in the program but can add some features by which I can convert this array of data into a file. The easiest thing would be to write the data into a file (in hard disk) and use it. But I will be working on thousands...
11
6639
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 transfer data across.On the serve I am using Socket 2 API (recv function to read bytes)and not using ..NET. I use FileStream to open the file on the pocket pc, then associate a BinaryReader object with the stream and call ReadBytes to read all the...
16
9112
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 kind of writing and reading best regard Ali.
0
3940
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. It is almost like it is trying to implement it's own COM interfaces... below is the header, and a link to the dll+code: Zip file with header, example, and DLL:...
34
3875
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
4780
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, sell price, and a taxable flag (a Y/N char) and then write this all out to a file (preferably just a plain old text file) and then read it in later so that I can keep a running inventory. The problem that I am running into is when I write to the...
4
1710
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 am trying to read a file with line line 8 characters wide, 88888888 it has unix line ending LF, but I am getting a segfault from the sscanf
0
9592
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9425
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10005
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9871
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6679
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5313
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5452
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3972
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3576
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.