473,403 Members | 2,183 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Re: Convert DataTable to Xml string variable...

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

"Jeroen Mostert" wrote:
Matthew Wells wrote:
"Arne Vajhøj" <ar**@vajhoej.dkwrote in message
news:47***********************@news.sunsite.dk...
ch*******@gmail.com 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
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.
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 ??
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).

--
J.
Jun 27 '08 #1
0 2827

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Eidolon | last post by:
I have a class which inherits from System.Data.DataTable. The code is basically (property procs omitted for clarity): Public Class DataTable() Inherits System.Data.DataTable Public Property...
5
by: jk | last post by:
I'm having trouble converting a datatable into xml, with resonse.write to aspx. I'm basically converting vb code that saved a recordset into a stream into c#, but the format is wrong. I've tried...
3
by: Rodusa | last post by:
I am looking for an example of how to save a DataTable into Base64 and vice-versa. I tried using Convert.ToBase64String(), but it says that it cannot convert DataTable to byte. Any help or pointing...
2
by: Steve | last post by:
I have a very simple datatable of 1 column which I retrieve from a database, call it 'data'. Dim data As Data.DataTable data = myobject.mymethod(parameter) I want to now turn this 'data' into...
3
by: Jon | last post by:
I'm learning about datatables. When using the example provided by MS in the ..NET Framework Class Library for DATATABLE (see below) I get an error on line 3 that says "Type expected". Is something...
5
by: manmit.walia | last post by:
Hello All, I am stuck on a conversion problem. I am trying to convert my application which is written in VB.NET to C# because the project I am working on currently is being written in C#. I tried...
5
by: Manikandan | last post by:
Hi, I have a datatable with rows. When I used datatable.select with values it is working properly, But when I use the select with variables it is not working. I tried with putting '(single...
1
by: pnbaocuong | last post by:
Dear All When I try to use convert function of MS sql server like this: String sql = " (employee_id = '" + employee_id + "') and ((convert(VARCHAR,w_date,103) = '" + w_date + "'))"; ...
9
by: myotheraccount | last post by:
Hello, Is there a way to convert a DataRow to a StringArray, without looping through all of the items of the DataRow? Basically, I'm trying to get the results of a query and put them into a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.