By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,370 Members | 1,534 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,370 IT Pros & Developers. It's quick & easy.

Re: Convert DataTable to Xml string variable...

P: n/a
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();

"Jeroen Mostert" wrote:
Matthew Wells wrote:
"Arne Vajhøj" <ar**@vajhoej.dkwrote in message
ch******* wrote:
On 23 Feb, 20:26, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
Matthew Wells <Matthew.We...@FirstByte.netwrote:
I'm trying to find a way to convert a datatable to xml and assign
to a string variable without saving to a file. Obviously I don't want
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
stream object, but I haven't come up with anything. How can I get this
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.
Yup, but turn off the unicode conversion... if I remeber correctly it
will otherwise produce some strange char at the beginning of the
BOM ??
I'm still a bit new to all this. Can you give me an example?

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

Jun 27 '08 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.