.NET Framework - Convert DataTable to Xml string variable...

Asked By Matthew Wells on 23-Feb-08 02:18 PM

I'm trying to find a way to convert a datatable to xml and assign that
to a string variable without saving to a file.  Obviously I don't want to
slow down the process by writing to disk, but all the methods I've found so
far (datatable.writexml) write to a file.  I think there's a way to use a
stream object, but I haven't come up with anything.  How can I get this done
without writing to disk?


Matthew Wells

Jon Skeet [C# MVP] replied on 23-Feb-08 02:26 PM
DataTable.WriteXml has a whole bunch of overloads, including ones which
write to a Stream, or a TextWriter.

You can use a MemoryStream or a StringWriter with these overloads to
keep it all in memory.

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
christer replied on 24-Feb-08 08:58 AM
n that
e a
s done

Yup, but turn off the unicode conversion... if I remeber correctly it
will otherwise produce some strange char at the beginning of the
arn replied on 23-Feb-08 03:08 PM
BOM ??

Matthew Wells replied on 23-Feb-08 03:30 PM
I am still a bit new to all this.  Can you give me an example?

Jeroen Mostert replied on 23-Feb-08 04:38 PM
string result;
using (StringWriter sw = new StringWriter()) {
result = sw.ToString();

If you don't actually need a string but read-only, processable XML, it's a
better idea to use MemoryStream and XPathDocument:

XPathDocument result;
using (MemoryStream ms = new MemoryStream()) {
ms.Position = 0;
result = new XPathDocument(ms);

Note that you can also directly create an XmlDataDocument from a DataSet
(rather than a DataTable).

Matthew Wells replied on 25-Feb-08 08:29 AM
That worked PERFECTLY!!!  Thank you so much.  My current situation requires
a string, but I'm interested in your second approach.  Where would I use

Thanks again.

Matthew Wells
rone matia replied on 07-May-08 01:13 AM
I have the same task to do but everytime I tried to parse my code I get a
null value returned after executing "dtMaterials.WriteXml(swMaterials);". I
am using the following code: Hope you can hep me out with this.


DataTable dtMaterials = new DataTable();
StringWriter swMaterials = new StringWriter();
swMaterials = null;
string strMaterials = string.Empty;

ConfiguratorDataContext dbConfigurator = new ConfiguratorDataContext();
var matchedMaterial = from mts in dbConfigurator.Materials
join ct in dbConfigurator.ColorTypes
on mts.ColorTypeID equals ct.ColorTypeID
join st in dbConfigurator.ShapeTypes
on mts.ShapeTypeID equals st.ShapeTypeID
join mto in
dbConfigurator.MaterialOrientationTypes on mts.MaterialID equals
select new
ColorName = ct.Name,
ShapeName = st.Name,
ImageTypeID = mto.ImageTypeID

IDbCommand command =
command.Connection = dbConfigurator.Connection;
IDataReader reader = command.ExecuteReader();
ConvertDataTable cdt = new ConvertDataTable();
cdt.FillDT(dtMaterials, reader);
catch (Exception ex)
string mess = ex.Message;

return strMaterials = swMaterials.ToString();
normah replied on 11-Aug-09 11:32 PM
You are required to be a member to post replies.  After logging in or becoming a member, you will be redirected back to this page.