Solved

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

Posted on 2014-03-07
3
372 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 74

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now