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

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

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?

Thanks.

Matthew Wells
Matthew.Wells@FirstByte.net




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
to
so
e a
s done
w.msmvps.com/jon.skeet

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

Arne
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?

Thanks.
Jeroen Mostert replied on 23-Feb-08 04:38 PM
string result;
using (StringWriter sw = new StringWriter()) {
dataTable.WriteXml(sw);
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()) {
dataTable.WriteXml(ms);
ms.Position = 0;
result = new XPathDocument(ms);
}

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

--
J.
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
that?

Thanks again.

Matthew Wells
Matthew.Wells@FirstByte.net
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.

Thanks.

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

try
{
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
mto.MaterialID
select new
{
mts.MaterialID,
mts.Name,
ColorName = ct.Name,
ShapeName = st.Name,
ImageTypeID = mto.ImageTypeID
};

IDbCommand command =
dbConfigurator.GetCommand(matchedMaterial);
command.Connection = dbConfigurator.Connection;
command.Connection.Open();
IDataReader reader = command.ExecuteReader();
ConvertDataTable cdt = new ConvertDataTable();
cdt.FillDT(dtMaterials, reader);
dtMaterials.WriteXml(swMaterials);
}
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.