Solved

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

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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

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…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

730 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