Solved

return SqlReader results as XML

Posted on 2013-11-20
2
291 Views
Last Modified: 2013-11-20
Hi,

I have an interface in a Web service that returns XML output. The output is queried from a MS SQL database by caling stored procedure. While the stored proc itself is not returning XML, and I don't have any control over it, I need to return the result set as XML.

Right now, I'm using  XmlWriter.

            
            StringBuilder sbLog = new StringBuilder();
            XmlWriter writer = XmlWriter.Create(sbLog);
            writer.WriteStartDocument();
            writer.WriteStartElement("LogSet");

            while (sqlRdr.Read())
            {
                writer.WriteStartElement("Log");

                writer.WriteStartElement("EntryID");
                writer.WriteValue(sqlRdr["EntryID"].ToString());
                writer.WriteEndElement();

               //and so on

Open in new window


Is there an easier, elegant way  to do this?

thanks!
0
Comment
Question by:olmuser
2 Comments
 
LVL 26

Accepted Solution

by:
Shaun Kline earned 500 total points
ID: 39662382
A similar question was asked on StackOverflow: (stackoverflow.com/questions/16768263/generate-xml-using-c-sharp-from-sql-server-data).

In this example, the data was returned into a DataTable using a SQLDataAdapter. From there, the methods WriteXml and WriteXmlSchema were used.
0
 
LVL 1

Author Closing Comment

by:olmuser
ID: 39662660
yeah I posted that question too :-)
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question