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

Convert XmlReader to string

I wish to receive some data from SqlServer as XML and write out result
to as a string.

Eg: cmd = new SqlCommand( "select * from blah for xml auto", con );

XmlReader result = cmd.ExecuteXmlReader( );

Console.Writeln( result.ToString( ) );

This XML is meant to be the input for an XSLT transform so I'm
interested in seeing the XML in a form similar to <row col1='blah'
col2='blah' />. The XML comes back ok, but I'm not sure how to convert
the XmlReader into a String in an efficient manner.

One way I know would be to load it into a dataset and then write out
as XML and another way would be to serialize / deserialize from an
object, but surely there is a simple way of converting the XmlReader
into a String.

Cheers Dave
Nov 16 '05 #1
2 17133
muesliflakes <mu**********@yahoo.com.au> wrote:
I wish to receive some data from SqlServer as XML and write out result
to as a string.

Eg: cmd = new SqlCommand( "select * from blah for xml auto", con );

XmlReader result = cmd.ExecuteXmlReader( );

Console.Writeln( result.ToString( ) );

This XML is meant to be the input for an XSLT transform so I'm
interested in seeing the XML in a form similar to <row col1='blah'
col2='blah' />. The XML comes back ok, but I'm not sure how to convert
the XmlReader into a String in an efficient manner.

One way I know would be to load it into a dataset and then write out
as XML and another way would be to serialize / deserialize from an
object, but surely there is a simple way of converting the XmlReader
into a String.


The easiest thing is to read the whole document in using
XmlDocument.Load(XmlReader) and then either write it out again using an
XmlWriter writing to a StringWriter, or something similar. (I haven't
tried calling OuterXml on an XmlDocument - that might do it for you in
a very straightforward way.)

Of course, this is a somewhat inefficient way of doing things. An
alternative is to have an XmlWriter writing to a StringWriter and use
XmlWriter.WriteNode (XmlReader, bool). I can't say I've tried that
either, to be honest - but it's worth having a go.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
d c
I would love to use the XmlDocument class but the problem is that this
class requires the XmlReader to return a well formed document.

My query which is to get the structure of a table:

select * from information_schema.columns where table_name = 'MtFeedback'
for xml auto

Does not return a well formed document, it only returns an XML fragment.

No Root Element:
<information_schema.columns TABLE_CATALOG="Mt" TABLE_SCHEMA="dbo"
TABLE_NAME="MtFeedback" COLUMN_NAME="MtFeedbackIy" ORDINAL_POSITION="1"
IS_NULLABLE="No " DATA_TYPE="int" NUMERIC_PRECISION="10"
NUMERIC_PRECISION_RADIX="10" NUMERIC_SCALE="0"/>

<information_schema.columns TABLE_CATALOG="Mt" TABLE_SCHEMA="dbo"
TABLE_NAME="MtFeedback" COLUMN_NAME="Title" ORDINAL_POSITION="2"
IS_NULLABLE="YES" DATA_TYPE="varchar" CHARACTER_MAXIMUM_LENGTH="50"
CHARACTER_OCTET_LENGTH="50" CHARACTER_SET_NAME="iso_1"
COLLATION_NAME="Latin1_General_CI_AS"/>

etc...
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #3

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

Similar topics

3
by: Convert TextBox.Text to Int32 Problem | last post by:
Need a little help here. I saw some related posts, so here goes... I have some textboxes which are designed for the user to enter a integer value. In "old school C" we just used the atoi function...
4
by: Ken Varn | last post by:
I have an unknown numeric Type object passed into a function. I want to run a conversion on a string to convert the string to that Type object and return an object of that type. Is there some way...
1
by: dongxm | last post by:
XslTransform xslt = new XslTransform(); xslt.Load(myXmlReader) the myXmlReader is a XmlReader,but i have String of xsl file's content... how to do,String convert XmlReader? thanks
15
by: Yifan | last post by:
Hi Does anybody know how to convert System::String* to char*? I searched the System::String class members and did not find any. Thanks Yifan
5
by: Ken Sturgeon | last post by:
Using VB.NET in VS2005, assume that you have a RegEx that matches a specific html tag; for instance "<title>.*?</title>" that returns a match "<title>SomeTitleHere</title>" and you want to convert...
3
by: priyanka | last post by:
Hi there, I want to convert a String into integer. I get the string from a file using : string argNum; getline(inputStream,argNum); I now need to convert argNum into integer.
1
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does 1+1 equal 11? or How do I convert a string to a number?...
2
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does 1+1 equal 11? or How do I convert a string to a number?...
12
by: aatif | last post by:
I want to convert a string of hex characters (2 hex chars = 1 byte), to ASCII. Hex chars include zeros (0x00) as well, which I want to include in ASCII string. hex string: 5000005355.... ASCII:...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.