HighTechTalks DotNet Forums  

Transform After WriteXml?

Dotnet Framework (ADO.net) microsoft.public.dotnet.framework.adonet


Discuss Transform After WriteXml? in the Dotnet Framework (ADO.net) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
coconet
 
Posts: n/a

Default Transform After WriteXml? - 12-12-2007 , 02:55 PM







I have a DataTable with many rows. I use WriteXml to emit to an
XmlWriter with an underlying StringWriter to put the final output into
a string.

I need to massage the XML output now. I don't know if it should be
done while the data is in the DataTable, during the XmlWriter writing
phase or after the information is in a string. I definitely don't want
to put the whole thing into an XML DOM instance because the XML is
currently 300mb in size.

The data looks like this:

<item>
<code>aaaa</code>
<info>bbbb</info>
<info2>cccc</info2>
</item>

For every "info2" element, I need to add these two sub-elements
(currently hardcoded):
<p:testbegin>2007-12-12T15:00:00</p:testbegin>
<p:testend>2007-12-13T15:00:00</p:testend>

I would also like to add a custom namespace to the final XML document
so instead of "code" I would like to have "p:code", and "p:info"
instead of "info", but I do not want to change "info2".

What is the best way to transform this from its DataTable origin?

Thanks.


Reply With Quote
  #2  
Old   
coconet
 
Posts: n/a

Default Re: Transform After WriteXml? - 12-12-2007 , 03:40 PM







In addition, if an element is empty, it should be removed from the
output.

On Wed, 12 Dec 2007 15:55:47 -0500, coconet <coconet (AT) community (DOT) nospam>
wrote:

Quote:
I have a DataTable with many rows. I use WriteXml to emit to an
XmlWriter with an underlying StringWriter to put the final output into
a string.

I need to massage the XML output now. I don't know if it should be
done while the data is in the DataTable, during the XmlWriter writing
phase or after the information is in a string. I definitely don't want
to put the whole thing into an XML DOM instance because the XML is
currently 300mb in size.

The data looks like this:

item
code>aaaa</code
info>bbbb</info
info2>cccc</info2
/item

For every "info2" element, I need to add these two sub-elements
(currently hardcoded):
p:testbegin>2007-12-12T15:00:00</p:testbegin
p:testend>2007-12-13T15:00:00</p:testend

I would also like to add a custom namespace to the final XML document
so instead of "code" I would like to have "p:code", and "p:info"
instead of "info", but I do not want to change "info2".

What is the best way to transform this from its DataTable origin?

Thanks.


Reply With Quote
  #3  
Old   
WenYuan Wang [MSFT]
 
Posts: n/a

Default Re: Transform After WriteXml? - 12-13-2007 , 02:15 AM



Hello coconet,

It seems you need more control than what DataTable.WriteXml method
provides. My suggestion is you may implement a custom WriterXML method by
yourself. Thereby, we can apply logics and rules into XML file directly.
Another idea is using XSL to transform the output from WirterXML method.

Hope this helps. Please feel free to update here, if there is anything
unclear. We are glad to assist you.

Have a great day,
Best regards,

Wen Yuan
Microsoft Online Community Support
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.


Reply With Quote
  #4  
Old   
coconet
 
Posts: n/a

Default Re: Transform After WriteXml? - 12-13-2007 , 10:06 AM



Please provide an example of using a custom WriterXML method. Please
provide an example of using XSL to transform the output.

Thanks.


On Thu, 13 Dec 2007 08:15:32 GMT, v-wywang (AT) online (DOT) microsoft.com
(WenYuan Wang [MSFT]) wrote:

Quote:
Hello coconet,

It seems you need more control than what DataTable.WriteXml method
provides. My suggestion is you may implement a custom WriterXML method by
yourself. Thereby, we can apply logics and rules into XML file directly.
Another idea is using XSL to transform the output from WirterXML method.

Hope this helps. Please feel free to update here, if there is anything
unclear. We are glad to assist you.

Have a great day,
Best regards,

Wen Yuan
Microsoft Online Community Support
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.


Reply With Quote
  #5  
Old   
WenYuan Wang [MSFT]
 
Posts: n/a

Default Re: Transform After WriteXml? - 12-14-2007 , 03:06 AM



Hello coconet,
Thanks for your reply,

It's not difficult to build a demo about the custom WriteXML method. I have
write a sample for you. But for XSL, I cannot found a sample closed to your
case. The following article provide a sample about how to use XSL to
transform the output from DataSet.WriteXML method as Excel formate. Hope
this helps.
http://www.123aspx.com/redir.aspx?res=36092
[Export dataset to Excel with XSLT in Asp.Net 2.0]

static void Main(string[] args)
{
System.Data.DataTable dt = new System.Data.DataTable();
dt.Columns.Add("code");
dt.Columns.Add("info");
dt.Columns.Add("info2");
dt.Rows.Add(new Object[] {"aaa","bbb","ccc" });
dt.Rows.Add(new Object[] { "aaa", "bbb", "ccc" });
dt.Rows.Add(new Object[] { "aaa", "bbb", "ccc" });
dt.Rows.Add(new Object[] { "aaa", "bbb",});
dt.TableName = "item";
dt.Namespace = "http://test.com";
WriteXML(dt, "testxml.xml");
}

static void WriteXML(System.Data.DataTable dt, string filename)
{
XmlWriter xtw = new XmlTextWriter(filename, null);
xtw.WriteStartDocument();
xtw.WriteStartElement(dt.TableName, null);
xtw.WriteAttributeString("xmlns","p",null, "http://p.p.p");
foreach (System.Data.DataRow dr in dt.Rows)
{
for (int i = 0; i < dt.Columns.Count; i++)
{
if (dr[i].ToString() == "")
continue;

if (dt.Columns[i].ColumnName == "info2")
{
xtw.WriteStartElement(dt.Columns[i].ColumnName);
xtw.WriteElementString("p", "testbegin",
"http://p.p.p", "2007-12-12T15:00:00");
xtw.WriteElementString("p", "testend",
"http://p.p.p", "2007-12-12T15:00:00");
xtw.WriteEndElement();
}
else
{
xtw.WriteElementString("p",
dt.Columns[i].ColumnName.ToString(), "http://p.p.p", dr[i].ToString());
}
}
}
xtw.WriteEndElement();
xtw.WriteEndDocument();
xtw.Flush();
xtw.Close();
}

Hope this helps,
Best regards,

Wen Yuan
Microsoft Online Community Support
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.


Reply With Quote
  #6  
Old   
WenYuan Wang [MSFT]
 
Posts: n/a

Default Re: Transform After WriteXml? - 12-18-2007 , 04:31 AM



Hello Coconet,

Is there anything else we can help with?
I haven't heard from you a couple of days. I just want to check if you have
resolved so far.

Have a great day,
Best regards,

Wen Yuan
Microsoft Online Community Support
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.


Reply With Quote
  #7  
Old   
Cor Ligthert[MVP]
 
Posts: n/a

Default Re: Transform After WriteXml? - 12-18-2007 , 11:01 PM



Coconet,

You are in my idea outside the rules of the XML Dataset.

The only way I could easily get this approach (I to wanted to filter out the
empty columns and change the dates to a less long format. Thiw was when I
was using this on Internet while most people had not fast connections) was
to enter the dataset in an arraylist (or whatever format of that today) and
filter it one by one using that. (A serialized dataset is in fact nothing
more than a txtfile)

Be aware that a dataset has only elements and no attributes or properties.

Cor

"coconet" <coconet (AT) community (DOT) nospam> schreef in bericht
news:17i0m3528ms78ge1j01alshfa24s7kj7sc (AT) 4ax (DOT) com...
Quote:
I have a DataTable with many rows. I use WriteXml to emit to an
XmlWriter with an underlying StringWriter to put the final output into
a string.

I need to massage the XML output now. I don't know if it should be
done while the data is in the DataTable, during the XmlWriter writing
phase or after the information is in a string. I definitely don't want
to put the whole thing into an XML DOM instance because the XML is
currently 300mb in size.

The data looks like this:

item
code>aaaa</code
info>bbbb</info
info2>cccc</info2
/item

For every "info2" element, I need to add these two sub-elements
(currently hardcoded):
p:testbegin>2007-12-12T15:00:00</p:testbegin
p:testend>2007-12-13T15:00:00</p:testend

I would also like to add a custom namespace to the final XML document
so instead of "code" I would like to have "p:code", and "p:info"
instead of "info", but I do not want to change "info2".

What is the best way to transform this from its DataTable origin?

Thanks.



Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.