XML writer in web service

E

E.Oosterhoorn

Hi,
I am trying to implement the following:
connect to data source, run stored procedure, store data in data table.
This all works fine, data table is accessed and the rows and columns are
writing their data into a string.
What I want to do is put XML data tags around the returned data, if I do
this as follows
<code>

GCS.DrugData = GCS.DrugData + "<ROW>" ;
foreach (DataColumn myColumn in myTable.Columns)
{
GCS.DrugData = GCS.DrugData + myRow[myColumn] + "," ;
}
GCS.DrugData = GCS.DrugData + "</ROW>" ;

</code>

The result is that the starting tag is created as <DrugData> but the
tags I have entered (<ROW>) are not recognised as XML. How do I enter
XML tags directly into the return string?

Thanks,
Erwin
 
M

Martin Honnen

E.Oosterhoorn said:
Hi,
I am trying to implement the following:
connect to data source, run stored procedure, store data in data table.
This all works fine, data table is accessed and the rows and columns are
writing their data into a string.
What I want to do is put XML data tags around the returned data, if I do
this as follows
<code>

GCS.DrugData = GCS.DrugData + "<ROW>" ;
foreach (DataColumn myColumn in myTable.Columns)
{
GCS.DrugData = GCS.DrugData + myRow[myColumn] + "," ;
}
GCS.DrugData = GCS.DrugData + "</ROW>" ;

</code>

The result is that the starting tag is created as <DrugData> but the
tags I have entered (<ROW>) are not recognised as XML. How do I enter
XML tags directly into the return string?

Can't you simply use the WriteXml method of a DataTable?
<URL:http://msdn2.microsoft.com/en-us/library/system.data.datatable.writexml.aspx>
 
E

E.Oosterhoorn

Martin said:
E.Oosterhoorn said:
Hi,
I am trying to implement the following:
connect to data source, run stored procedure, store data in data
table. This all works fine, data table is accessed and the rows and
columns are writing their data into a string.
What I want to do is put XML data tags around the returned data, if I
do this as follows
<code>

GCS.DrugData = GCS.DrugData + "<ROW>" ;
foreach (DataColumn myColumn in myTable.Columns)
{
GCS.DrugData = GCS.DrugData + myRow[myColumn] + "," ;
}
GCS.DrugData = GCS.DrugData + "</ROW>" ;

</code>

The result is that the starting tag is created as <DrugData> but the
tags I have entered (<ROW>) are not recognised as XML. How do I enter
XML tags directly into the return string?

Can't you simply use the WriteXml method of a DataTable?
<URL:http://msdn2.microsoft.com/en-us/library/system.data.datatable.writexml.aspx>
thanks,
no, I am using .Net framework 1. and that is with framework 3.0
That should be incredibly easy, if it was available.
 
M

Martin Honnen

E.Oosterhoorn said:
no, I am using .Net framework 1. and that is with framework 3.0
That should be incredibly easy, if it was available.

WriteXml for a DataTable is also available in the .NET framework 2.0.
With 1.1 DataSet has a WriteXml method
<URL:http://msdn2.microsoft.com/en-us/library/system.data.dataset.writexml(VS.71).aspx>
so you could add your table you want to write to a DataSet and write
that out. Or you need to use XmlTextWriter to generate the XML yourself.
 
E

E.Oosterhoorn

Martin said:
WriteXml for a DataTable is also available in the .NET framework 2.0.
With 1.1 DataSet has a WriteXml method
<URL:http://msdn2.microsoft.com/en-us/library/system.data.dataset.writexml(VS.71).aspx>
so you could add your table you want to write to a DataSet and write
that out. Or you need to use XmlTextWriter to generate the XML yourself.
Martin, thanks for your help,

I have been looking at these methods, but I haven't used this type of
coding for a while, would you mind pointing me in the right direction
how I can take the data set and write this to a temp stream(?) to add to
the return tags in the web service.
I have a structure that contains strings where the data is to be
returned to the requester. The web service code creates these tags but I
want to add the result from the stored proc. inside these tags as XML.
What do I need to do to create a stream, write to the stream and read
that again to add to the string.
Erwin
 
M

Martin Honnen

E.Oosterhoorn said:
I have been looking at these methods, but I haven't used this type of
coding for a while, would you mind pointing me in the right direction
how I can take the data set and write this to a temp stream(?) to add to
the return tags in the web service.
I have a structure that contains strings where the data is to be
returned to the requester. The web service code creates these tags but I
want to add the result from the stored proc. inside these tags as XML.
What do I need to do to create a stream, write to the stream and read
that again to add to the string.

It is not clear to me what kind of XML you want to create. If you have a
DataTable and want to write it out to an XmlWriter or TextWriter with
..NET 1.1 then the following should help:

static void WriteTableXml(DataTable table, string rootElementName,
TextWriter textWriter)
{
WriteTableXml(table, rootElementName, new XmlTextWriter(textWriter));
}

static void WriteTableXml(DataTable table, string rootElementName,
XmlWriter xmlWriter)
{
xmlWriter.WriteStartElement(rootElementName);
foreach (DataRow row in table.Rows)
{

xmlWriter.WriteStartElement(XmlConvert.EncodeLocalName(table.TableName));
foreach (DataColumn col in table.Columns)
{

xmlWriter.WriteElementString(XmlConvert.EncodeLocalName(col.ColumnName),
row[col].ToString());
}
xmlWriter.WriteEndElement();
}
xmlWriter.WriteEndElement();
}

Example use is as follows:


DataTable table = new DataTable("table1");
DataColumn col1 = new DataColumn("col1", typeof(int));
table.Columns.Add(col1);
for (int i = 1; i <= 10; i++)
{
DataRow row = table.NewRow();
row[0] = i;
table.Rows.Add(row);
}

WriteTableXml(table, "root", Console.Out);
 

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

XML and SQL database 3
xml and xml schema 4
XML to Dictionary using LINQ 2
Change in XML format 1
puzzle question 2 6
where clausule in xml file 1
XML and collectionbase 3
Loop with Step 1

Top