Solved

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

Posted on 2014-03-07
3
393 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 63

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

689 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