473,789 Members | 2,707 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SqlCommand.Exec uteXmlReader() - 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.Exec uteXmlReader() 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( connectionStrin g ) )
{
sc.Open();
SqlCommand cmd = new SqlCommand( cmdString, sc );

XmlReader xr = cmd.ExecuteXmlR eader();
ds.ReadXmlSchem a( xr );
ds.ReadXml( xr, XmlReadMode.Fra gment );
sc.Close();
}

ds.DataSetName = "root";
ds.WriteXml( @"C:\Test.xm l" );
ds.WriteXmlSche ma( @"C:\Test.xs d" );
Nov 17 '05 #1
4 15169

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******** ******@TK2MSFTN GP14.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.Exec uteXmlReader() 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( connectionStrin g ) )
{
sc.Open();
SqlCommand cmd = new SqlCommand( cmdString, sc );

XmlReader xr = cmd.ExecuteXmlR eader();
ds.ReadXmlSchem a( xr );
ds.ReadXml( xr, XmlReadMode.Fra gment );
sc.Close();
}

ds.DataSetName = "root";
ds.WriteXml( @"C:\Test.xm l" );
ds.WriteXmlSche ma( @"C:\Test.xs d" );

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( connectionStrin g ) )
{
sc.Open();
SqlCommand cmd = new SqlCommand( cmdString, sc );
xd.Load( cmd.ExecuteXmlR eader() );
}
XmlWriter xw = new XmlTextWriter( new StreamWriter( @"C:\Test.xm l" ) );
xd.WriteTo( xw );

Thanks again...

- Steve

"Dan Bass" <Not Listed> wrote in message
news:un******** ******@TK2MSFTN GP15.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( connectionStrin g );
comm.CommandTyp e = SqlXmlCommandTy pe.Sql;
comm.CommandTex t = "SELECT * FROM TestTable order by column1 FOR XML AUTO,
XMLDATA";
comm.OutputEnco ding = "UTF-8";
comm.RootTag = "root";
FileStream xmlFile = new FileStream(@"c: \test.xml", FileMode.Create ,
FileAccess.Writ e);
comm.ExecuteToS tream(xmlFile);
xmlFile.Close() ;

"Steve Harclerode" <Ca************ ***@hot.mail.co m> wrote in message
news:%2******** ********@TK2MSF TNGP15.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.co m> wrote in message
news:e6******** ******@TK2MSFTN GP09.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( connectionStrin g );
comm.CommandTyp e = SqlXmlCommandTy pe.Sql;
comm.CommandTex t = "SELECT * FROM TestTable order by column1 FOR XML AUTO,
XMLDATA";
comm.OutputEnco ding = "UTF-8";
comm.RootTag = "root";
FileStream xmlFile = new FileStream(@"c: \test.xml", FileMode.Create ,
FileAccess.Writ e);
comm.ExecuteToS tream(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
7738
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 @Type='Rushing'> <LEADER @Carries=''/> </LEADERBOARD> <LEADERBOARD @Type='QBRating'> <LEADER @Rating='' @CompletionPct=''/>
2
4939
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"; c.Open(); SqlCommand command = c.CreateCommand(); command.CommandType = CommandType.Text; command.CommandText = "select Customers.customerid, customers.companyname, " +
5
6800
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 .net i'm having problems loading this up. I've now tried installing sqlxml managed classes and the following appears to work when stepping through, but the result just disappears.
10
18547
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 release? I would basically prefer to skip invoking Dispose() as this will free me from determining when the usage actually has finished.
8
2529
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
3973
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 returns "<CustomerParameters Name="Www.Mail.Generel"/><CustomerParameters Name="Www.Mail.Updates"/>" I'm using following to read the result returned from the SqlConnection
1
5595
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 command multiple times, do not reuse the same SqlCommand object to execute different commands. 1) You do not need to explicitly open or close the database connection. The SqlDataAdapter Fill method opens the database connection and then closes the...
1
7258
by: job | last post by:
how is it possible to serialize/de-serialize a SqlCommand?
1
4929
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 connectionString) { using (SqlConnection connection = new SqlConnection(connectionString)) {
0
9666
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
9511
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,...
0
10410
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10200
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9984
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
6769
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
5418
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...
1
4093
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
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.