Solved

Need to return the XML from a Dataset as a string from a C# method

Posted on 2014-03-07
3
379 Views
Last Modified: 2014-03-10
I need to return as a string the XML from a DataSet

Stream returnXML = new MemoryStream();
            returnDS.WriteXml(returnXML);                       
            return returnXML   //How do I convert this to a return string?

Open in new window

0
Comment
Question by:Bruce
3 Comments
 
LVL 11

Assisted Solution

by:MajorBigDeal
MajorBigDeal earned 225 total points
ID: 39913486
I didn't compile this but hopefully it will point you in a good direction:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(responseReader);

StringWriter stringWriter = new StringWriter();
XmlTextWriter xmlTextWriter = new XmlTextWriter(stringWriter);
xmlTextWriter.Formatting = Formatting.Indented; // remove if pretty print not wanted
xmlDoc.WriteTo(xmlTextWriter);
xmlTextWriter.Flush();
return stringWriter.ToString();
0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 200 total points
ID: 39913492
Use the DataSet method GetXml() as follows.

return returnDS.GetXml();

Therefor no need for the memory stream.
0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 75 total points
ID: 39913511
You can use the Encoding class for this:

e.g.

using System.Text;

...

returnXML.Position = 0;

return Encoding.UTF8.GetString(returnXML.ToArray());

Open in new window

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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

772 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