Convert DataTable to Xml string variable...

M

Matthew Wells

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
(e-mail address removed)
 
J

Jon Skeet [C# MVP]

Matthew Wells said:
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?

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.
 
C

christery

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.

Yup, but turn off the unicode conversion... if I remeber correctly it
will otherwise produce some strange char at the beginning of the
"file"..
//CY
 
A

Arne Vajhøj

Yup, but turn off the unicode conversion... if I remeber correctly it
will otherwise produce some strange char at the beginning of the
"file"..

BOM ??

Arne
 
J

Jeroen Mostert

Matthew said:
I'm still a bit new to all this. Can you give me an example?

Thanks.
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).
 
M

Matthew Wells

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
(e-mail address removed)
 
R

rone matias

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();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top