Transform After WriteXml?

  • Thread starter Thread starter coconet
  • Start date Start date
C

coconet

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.
 
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.
 
Please provide an example of using a custom WriterXML method. Please
provide an example of using XSL to transform the output.

Thanks.
 
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.ToString() == "")
continue;

if (dt.Columns.ColumnName == "info2")
{
xtw.WriteStartElement(dt.Columns.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.ColumnName.ToString(), "http://p.p.p", dr.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.
 
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.
 
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
 
Back
Top