473,396 Members | 1,724 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.

SqlCommand.ExecuteXmlReader() - How to Use?

I tried this on another newsgroup but no nibbles -- I'm hoping someone might
be able to help on this csharp group --

I'm trying to use SqlCommand.ExecuteXmlReader() to write data to an XML
file. Most of the code is below. The result I'm seeing is that the schema
file is written and looks good, but the XML file has no data other than an
empty <root> node. Is there something simple I'm missing? BTW, running the
same query in query analyzer returns 7 nodes.

Thanks --
Steve

--------------------

string cmdString = "SELECT * FROM TestTable order by column1 FOR XML
AUTO, XMLDATA";
DataSet ds = new DataSet();

using ( SqlConnection sc = new SqlConnection( connectionString ) )
{
sc.Open();
SqlCommand cmd = new SqlCommand( cmdString, sc );

XmlReader xr = cmd.ExecuteXmlReader();
ds.ReadXmlSchema( xr );
ds.ReadXml( xr, XmlReadMode.Fragment );
sc.Close();
}

ds.DataSetName = "root";
ds.WriteXml( @"C:\Test.xml" );
ds.WriteXmlSchema( @"C:\Test.xsd" );
Nov 17 '05 #1
4 15138

An educated guess, but what is probably happening is the first node only is
being read into the data set on ReadXml...

I'd suggest using an XmlDocument object instead for what you're doing:
see this MSDN help: http://tinyurl.com/7pbjm
"Steve Harclerode" <Li*************@hot.mail.com> wrote in message
news:Ow**************@TK2MSFTNGP14.phx.gbl...
I tried this on another newsgroup but no nibbles -- I'm hoping someone
might be able to help on this csharp group --

I'm trying to use SqlCommand.ExecuteXmlReader() to write data to an XML
file. Most of the code is below. The result I'm seeing is that the schema
file is written and looks good, but the XML file has no data other than an
empty <root> node. Is there something simple I'm missing? BTW, running the
same query in query analyzer returns 7 nodes.

Thanks --
Steve

--------------------

string cmdString = "SELECT * FROM TestTable order by column1 FOR XML
AUTO, XMLDATA";
DataSet ds = new DataSet();

using ( SqlConnection sc = new SqlConnection( connectionString ) )
{
sc.Open();
SqlCommand cmd = new SqlCommand( cmdString, sc );

XmlReader xr = cmd.ExecuteXmlReader();
ds.ReadXmlSchema( xr );
ds.ReadXml( xr, XmlReadMode.Fragment );
sc.Close();
}

ds.DataSetName = "root";
ds.WriteXml( @"C:\Test.xml" );
ds.WriteXmlSchema( @"C:\Test.xsd" );

Nov 17 '05 #2
Thanks for the reply.

Unfortunately, after studying the XmlDocument class definition for a while I
don't see how it could help me. I can get that it's an abstraction of an Xml
document, but I don't see what I would need to 1) read the xml 2) write the
document, and 3) write the schema. Actually, if I could do 1), probably 2)
would work, but #3) I don't see any support for in this class. Anyway, below
is the code I have now. It gives an exception on xd.Load() :
"This document already has a DocumentElement node."
Which presumably happens when it discovers that SQL is yielding XML
fragments with no root node.

----------

string cmdString = "SELECT * FROM TestTable order by column1 FOR XML AUTO,
XMLDATA";
DataSet ds = new DataSet();
XmlDocument xd = new XmlDocument();
using ( SqlConnection sc = new SqlConnection( connectionString ) )
{
sc.Open();
SqlCommand cmd = new SqlCommand( cmdString, sc );
xd.Load( cmd.ExecuteXmlReader() );
}
XmlWriter xw = new XmlTextWriter( new StreamWriter( @"C:\Test.xml" ) );
xd.WriteTo( xw );

Thanks again...

- Steve

"Dan Bass" <Not Listed> wrote in message
news:un**************@TK2MSFTNGP15.phx.gbl...

An educated guess, but what is probably happening is the first node only
is being read into the data set on ReadXml...

I'd suggest using an XmlDocument object instead for what you're doing:
see this MSDN help: http://tinyurl.com/7pbjm


Nov 17 '05 #3
Okay, here's some code that fulfills 1) and 2) below. It uses the
Microsoft.Data.SqlXml class, which I had to download from "you know where".
However the output file (test.xml) also contains the schema, and I would
prefer that the schema be written to a separate file. Any suggestions?

SqlXmlCommand comm = new SqlXmlCommand( connectionString );
comm.CommandType = SqlXmlCommandType.Sql;
comm.CommandText = "SELECT * FROM TestTable order by column1 FOR XML AUTO,
XMLDATA";
comm.OutputEncoding = "UTF-8";
comm.RootTag = "root";
FileStream xmlFile = new FileStream(@"c:\test.xml", FileMode.Create,
FileAccess.Write);
comm.ExecuteToStream(xmlFile);
xmlFile.Close();

"Steve Harclerode" <Ca***************@hot.mail.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
Thanks for the reply.

Unfortunately, after studying the XmlDocument class definition for a while
I don't see how it could help me. I can get that it's an abstraction of an
Xml document, but I don't see what I would need to 1) read the xml 2)
write the document, and 3) write the schema. Actually, if I could do 1),
probably 2) would work, but #3) I don't see any support for in this class.
Anyway, below is the code I have now. It gives an exception on xd.Load() :
"This document already has a DocumentElement node."
Which presumably happens when it discovers that SQL is yielding XML
fragments with no root node.


Nov 17 '05 #4
I finally see how to write the schema to a separate file, by creating a
DataSet from the XML file using .ReadXmlSchema(), and then writing to an XSD
file using .WriteXmlSchema().

If there's a way to do this with an XmlDocument, I would be interested. So
far I have not identified any good reasons to use an XmlDocument.

Thanks, Steve

"Steve Harclerode" <Ca***************@hot.mail.com> wrote in message
news:e6**************@TK2MSFTNGP09.phx.gbl...
Okay, here's some code that fulfills 1) and 2) below. It uses the
Microsoft.Data.SqlXml class, which I had to download from "you know
where". However the output file (test.xml) also contains the schema, and I
would prefer that the schema be written to a separate file. Any
suggestions?

SqlXmlCommand comm = new SqlXmlCommand( connectionString );
comm.CommandType = SqlXmlCommandType.Sql;
comm.CommandText = "SELECT * FROM TestTable order by column1 FOR XML AUTO,
XMLDATA";
comm.OutputEncoding = "UTF-8";
comm.RootTag = "root";
FileStream xmlFile = new FileStream(@"c:\test.xml", FileMode.Create,
FileAccess.Write);
comm.ExecuteToStream(xmlFile);
xmlFile.Close();

Nov 17 '05 #5

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

Similar topics

3
by: Mark Miller | last post by:
I have an sp that outputs multiple xml fragments w/ no root. The sp calls individual sp's to output the correct set of data and each "type" has different fields. ex.: <LEADERBOARD...
2
by: Dylan Phillips | last post by:
A strang error is occurring when I run the following code: SqlConnection c = new SqlConnection(); c.ConnectionString = "Initial Catalog=Northwind;user id=sa;password=kat1ie;Data Source=server";...
5
by: adolf garlic | last post by:
Im trying to return xml from sql. The xml is made up of different fragments, some using FOR XML ... syntax. The result is a valid xml doc. There is a working stored proc that returns the xml In...
10
by: Henrik Dahl | last post by:
Hello! After I've finished using an instance of the SqlCommand class, should I then invoke Dispose() on the instance. I suppose so, as there is a Dispose method, but what does it actually...
8
by: terrorix | last post by:
I also posted this article at: microsoft.public.dotnet.framework.aspnet -------- Hi, I Have this sql select(MS SQL): select a.ID, b.ID from Table as a
1
by: Soren Jorgensen | last post by:
Hi, When executing following statement "select distinct Name from CustomerParameters for xml auto" - XmlReader reads "<customerparameters name="Www.Mail.Generel" />" - but SQL Query Manager...
1
by: Tom | last post by:
Hello guys Please have a look on following paragraph from ".NET Data Access Architecture Guide". ''''''''''' Although you can repeatedly use the same SqlCommand object to execute the same...
1
by: job | last post by:
how is it possible to serialize/de-serialize a SqlCommand?
1
by: Risen | last post by:
Hi, When I use sqlcommand.ExecuteXmlReader to receive Sql Server 2005's table data, it not work! my code is below: private static void CreateXMLReader(string queryString,string...
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: 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...
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
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...
0
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...

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.